VB Method to create a zip file for a folder - vb.net

I am very new to VB programming. I am trying to create a zip file using vb with sub folders in it, that one of the sub folders need not be included in the zip file created.

Private Function Compress(ByVal fileToCompress As FileInfo) As Boolean
Dim _Compress As Boolean
Using originalFileStream As FileStream = fileToCompress.OpenRead()
If (File.GetAttributes(fileToCompress.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".gz" Then
Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".gz")
Using compressionStream As GZipStream = New GZipStream(compressedFileStream, CompressionMode.Compress)
originalFileStream.CopyTo(compressionStream)
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString())
End Using
End Using
End If
End Using
_Compress = File.Exists(fileToCompress.FullName + ".gz")
Return _Compress
End Function

Related

VB.NET writing comments to jpeg file programmatically

I searched stackoverflow and I realized that GetPropertyItem and SetPropertyItem can edit comments in JPEG file
Dim images As Image = System.Drawing.Image.FromFile("C:\\Sample.jpeg")
Dim MSGF As New ArrayList
Dim ID() As String = {"hello ","i am here"}
Dim propItem As PropertyItem = images.GetPropertyItem(40092)
Dim encoderParameters As New EncoderParameters(1)
encoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 100L)
For i = 0 To ID.Length - 1
Dim TEMP As String = ID(i)
For II = 0 To TEMP.Length - 1
MSGF.Add(Convert.ToInt32(TEMP(II)))
Next
Next
For i = 0 To MSGF.Count - 1
propItem.Value.SetValue(Convert.ToByte(MSGF(i)), i)
Next
images.SetPropertyItem(propItem)
images.Save(TextBox1.Text & "\" & "1" & TextBox2.Text)
What I realized was I can get comments from jpeg file by GetPropertyItem. However, comments is based on the ascii code. Therefore, I was trying to convert comment that I wanted to insert to ascii code.
propItem.Value.SetValue(Convert.ToByte(MSGF(i)), i)
This part was actually changed comments which already existed in the jpeg file.
However, if there is no comments in jpeg file, propItem.value.setValue doesn't work because there is nothing to edit.
Is there anyway to just add comments to jpeg file?
Based on this answer in C#, it could be as simple as this:
Dim jpeg = New JpegMetadataAdapter(pathToJpeg)
jpeg.Metadata.Comment = "Some comments"
jpeg.Metadata.Title = "A title"
jpeg.Save()
' Saves the jpeg in-place
jpeg.SaveAs(someNewPath)
' Saves with a new path
Here is the class:
Public Class JpegMetadataAdapter
Private ReadOnly path As String
Private frame As BitmapFrame
Public ReadOnly Metadata As BitmapMetadata
Public Sub New(path As String)
Me.path = path
frame = getBitmapFrame(path)
Metadata = DirectCast(frame.Metadata.Clone(), BitmapMetadata)
End Sub
Public Sub Save()
SaveAs(path)
End Sub
Public Sub SaveAs(path As String)
Dim encoder As New JpegBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(frame, frame.Thumbnail, Metadata, frame.ColorContexts))
Using stream As Stream = File.Open(path, FileMode.Create, FileAccess.ReadWrite)
encoder.Save(stream)
End Using
End Sub
Private Function getBitmapFrame(path As String) As BitmapFrame
Dim decoder As BitmapDecoder = Nothing
Using stream As Stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad)
End Using
Return decoder.Frames(0)
End Function
End Class

How to zip the folders and its sub-folders in vb.net 2.0?

I have folder in the name of Main1. And inside that folders, i have multiple folders. Like
Folders1
Folders2
Folders3
Folders4
Each folders have their own files. My requirement is to zip "Main1" folder including all the sub-folders and their files.
I dont want to use any third part tools. I'm planning to use namespace System.Compression with Gzip. Please guys advice.
ZipFile.CreateFromDirectory() can easily do this for you. Just pass it the path of your Day1 folder and it will zip the entire folder to a zip file. You can iterate over all the Day folders using System.IO.Directory class.
(Just realized that you want to stick to .NET Fx 2.0. There is no direct way of doing this in that version. You must either use 3rd-party lib, which you don't want to, or do low-level stuff).
Edit
If you're really inclined towards doing it by hand, here is a crude way:
Get the list of all directories (recursive) in your Day directory; call it DirList.
Get the list of all files (recursive) in your Day directory; call it FilesList.
Create a Dictionary(Of String, String) and store the name of each file in FilesList and the BASE64 representation of its contents; name as Key, content as Value.
Save the Dictionary to an XML file using .NET's built-in XML serialization.
At the very start of the file, inject the contents of DirList.
Save the file again. (you could do steps 4-6 in a single step too).
Read this file as binary and use GZip to compress the entire content.
Write it to a zip file.
To uncompress this file:
Open the file and use GZip to decompress and get your entire content.
Grab the list of directories from the top and create all recursively.
Read the remaining section in its entirety and use XML Serialization to create your Dictionary object from it.
Iterate through the Dictionary and create files using the Key part and then inject contents into the files by converting their Value from BASE64 back to binary.
Let me know if you have questions about any of these steps.
Edit 2
The following code is compiled against .NET 2.0 and will compress and decompress a directory:
Public Function ZipDirectory(DirPath As String) As Byte()
If Not Directory.Exists(DirPath) Then Return Nothing
Dim Directories = Directory.GetDirectories(DirPath, "*", SearchOption.AllDirectories)
Dim Files = Directory.GetFiles(DirPath, "*", SearchOption.AllDirectories)
Dim X As New XmlDocument
Dim RootNode = X.CreateElement("Content")
Dim DirsNode = X.CreateElement("Directories")
Dim FilesNode = X.CreateElement("Directories")
X.AppendChild(RootNode)
RootNode.AppendChild(DirsNode)
RootNode.AppendChild(FilesNode)
For Each d In Directories
Dim DirNode = X.CreateElement("Directory")
Dim PathAttrib = X.CreateAttribute("Path")
PathAttrib.Value = d.Replace(DirPath & "\", "") 'Create relative paths
DirNode.Attributes.Append(PathAttrib)
DirsNode.AppendChild(DirNode)
Next
For Each f In Files
Dim FileNode = X.CreateElement("File")
Dim PathAttrib = X.CreateAttribute("Path")
PathAttrib.Value = f.Replace(DirPath & "\", "") 'Create relative paths
FileNode.Attributes.Append(PathAttrib)
FileNode.InnerText = Convert.ToBase64String(File.ReadAllBytes(f))
FilesNode.AppendChild(FileNode)
Next
Using Mem As New MemoryStream()
X.Save(Mem)
Dim AllContentsAsByteArray = Mem.ToArray()
Dim CompressedContent = CompressArray(AllContentsAsByteArray)
Return CompressedContent
End Using
End Function
Public Sub UnzipDirectory(compressed() As Byte, outputPath As String)
If Not Directory.Exists(outputPath) Then Directory.CreateDirectory(outputPath)
Dim Uncompressed = DecompressArray(Compressed)
Dim X As New XmlDocument
Using Mem As New MemoryStream(Uncompressed)
X.Load(Mem)
Dim RootNode = X.FirstChild
Dim DirsNode = RootNode.FirstChild
Dim FilesNode = RootNode.FirstChild.NextSibling
For Each ChildDir In DirsNode.ChildNodes
Directory.CreateDirectory(Path.Combine(outputPath, DirectCast(ChildDir, XmlNode).Attributes.Item(0).Value))
Next
For Each ChildFile In FilesNode.ChildNodes
Dim FilePath = Path.Combine(outputPath, DirectCast(ChildFile, XmlNode).Attributes.Item(0).Value)
Dim Content = Convert.FromBase64String(DirectCast(ChildFile, XmlNode).InnerText)
File.WriteAllBytes(FilePath, Content)
Next
End Using
End Sub
Private Function CompressArray(ByVal content() As Byte) As Byte()
Using outFile As New MemoryStream()
Using Compress As New GZipStream(outFile, CompressionMode.Compress)
Compress.Write(content, 0, content.Length)
End Using
Return outFile.ToArray()
End Using
End Function
Private Function DecompressArray(ByVal content() As Byte) As Byte()
Using outFile As New MemoryStream()
Using inFile As New MemoryStream(content)
Using Compress As New GZipStream(inFile, CompressionMode.Decompress)
Dim buffer(1023) As Byte
Dim nRead As Integer
Do
nRead = Compress.Read(buffer, 0, buffer.Length)
outFile.Write(buffer, 0, nRead)
Loop While nRead > 0
End Using
End Using
Return outFile.ToArray()
End Using
End Function
The code should be used like this:
'To zip a directory
Dim Compressed = ZipDirectory("C:\SomeDir")
File.WriteAllBytes("C:\somedir.zip", Compressed)
'To unzip a zipped file
Dim Compressed = File.ReadAllBytes("C:\somedir.zip")
UnzipDirectory(Compressed, "C:\SomeDir2")
Below code is using System.IO.Compression
Example from MSDN site:
Link
Imports System
Imports System.Collections.Generic
imports System.IO
imports System.IO.Compression
Public Class CompressionSnippet
Public Shared Sub Main()
Dim path As String = "test.txt"
' Create the text file if it doesn't already exist.
If Not File.Exists(path) Then
Console.WriteLine("Creating a new test.txt file")
Dim text() As String = {"This is a test text file.", _
"This file will be compressed and written to the disk.", _
"Once the file is written, it can be decompressed", _
"imports various compression tools.", _
"The GZipStream and DeflateStream class use the same", _
"compression algorithms, the primary difference is that", _
"the GZipStream class includes a cyclic redundancy check", _
"that can be useful for detecting data corruption.", _
"One other side note: both the GZipStream and DeflateStream", _
"classes operate on streams as opposed to file-based", _
"compression data is read on a byte-by-byte basis, so it", _
"is not possible to perform multiple passes to determine the", _
"best compression method. Already compressed data can actually", _
"increase in size if compressed with these classes."}
File.WriteAllLines(path, text)
End If
Console.WriteLine("Contents of {0}", path)
Console.WriteLine(File.ReadAllText(path))
CompressFile(path)
Console.WriteLine()
UncompressFile(path + ".gz")
Console.WriteLine()
Console.WriteLine("Contents of {0}", path + ".gz.txt")
Console.WriteLine(File.ReadAllText(path + ".gz.txt"))
End Sub
Public Shared Sub CompressFile(ByVal path As String)
Dim sourceFile As FileStream = File.OpenRead(path)
Dim destinationFile As FileStream = File.Create(path + ".gz")
Dim buffer(sourceFile.Length) As Byte
sourceFile.Read(Buffer, 0, Buffer.Length)
Using output As New GZipStream(destinationFile, _
CompressionMode.Compress)
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name, _
destinationFile.Name, False)
output.Write(buffer, 0, buffer.Length)
End Using
' Close the files.
sourceFile.Close()
destinationFile.Close()
End Sub
Public Shared Sub UncompressFile(ByVal path As String)
Dim sourceFile As FileStream = File.OpenRead(path)
Dim destinationFile As FileStream = File.Create(path + ".txt")
' Because the uncompressed size of the file is unknown,
' we are imports an arbitrary buffer size.
Dim buffer(4096) As Byte
Dim n As Integer
Using input As New GZipStream(sourceFile, _
CompressionMode.Decompress, False)
Console.WriteLine("Decompressing {0} to {1}.", sourceFile.Name, _
destinationFile.Name)
n = input.Read(buffer, 0, buffer.Length)
destinationFile.Write(buffer, 0, n)
End Using
' Close the files.
sourceFile.Close()
destinationFile.Close()
End Sub
End Class
Truly you would be better off using SharpLibZip

How to get single file from zip file?

I want to get just one file from .zip file in VB.NET. I don't need to extract all of .zip file, just one file.
I'm working with framework 4.5.
.NET Framework 4.5 has ZipFile class which can do this for you. This code should get you started:
Dim zipPath As String = "Sample.zip"
Using archive = ZipFile.Open(zipPath, ZipArchiveMode.Read)
Dim entry = archive.GetEntry("MyFile.pdf")
Using reader As New BinaryReader(entry.Open())
System.IO.File.WriteAllBytes("MyFile.pdf", ReadAllBytes(reader))
End Using
End Using
ReadAllBytes() is a helper method that fetches all bytes from a binary stream:
Public Shared Function ReadAllBytes(reader As BinaryReader) As Byte()
Const bufferSize As Integer = 4096
Using ms As New MemoryStream()
Dim buffer(bufferSize) As Byte
Dim count As Integer
Do
count = reader.Read(buffer, 0, buffer.Length)
If count > 0 Then ms.Write(buffer, 0, count)
Loop While count <> 0
Return ms.ToArray()
End Using
End Function
Make sure you're using .NET Framework 4.5 or above and that you have included references to System.IO.Compression and System.IO.Compression.FileSystem.
try with this code with the help of DotNetZip
Using zip As ZipFile = ZipFile.Read(ExistingZipFile)
Dim e As ZipEntry = zip("DocumentToFind.txt")
e.Extract(OutputStream)
End Using
otherwise you can use ZipArchiveClass in this way
Using zip As ZipArchive = ZipFile.Open(zipfile, ZipArchiveMode.Read)
Dim file = zip.Entries.Where(Function(x) x.Name = "fileToFind")
If file IsNot Nothing Then
file.ExtractToFile("yourFile")
End If
End Using
This will allow you to read txt files from a zip line by line
Dim zipPath As String = "ZIP FILE LOCATION"
Using zipStream = New FileStream(last_pafx23_open, FileMode.Open)
Using archive = New ZipArchive(zipStream, ZipArchiveMode.Read)
For Each ent In archive.Entries
MsgBox(ent.ToString)
Using stream = ent.Open()
Using reader = New StreamReader(stream)
While Not reader.EndOfStream
MsgBox(reader.ReadLine)
End While
End Using
End Using
Next
End Using
End Using
Skip the BinaryReader w/ ReadAllBytes() helper function, use ExtractToFile() instead:
Imports System.IO.Compression
Using archive = ZipFile.Open("Sample.zip", ZipArchiveMode.Read)
Dim entry = archive.GetEntry("MyFile.pdf")
If entry IsNot Nothing then entry.ExtractToFile("MyFile.pdf")
End Using
Still needs the references to System.IO.Compression and System.IO.Compression.FileSystem, of course.

Search engine in vb.net

I am building a search engine in vb.net which would have to search for a word entered by the user in 40 text files within the project directory.
It should return the results as the total number of matches (text files) and the number of times this word is in each file. Any suggestions for a start would be grateful.
Regards.
get a list of the files in the directory with something like: Directory.GetFiles(ProjectDir, "*.*"), then read each file in the list like this:
Dim sr As StreamReader = New StreamReader(fileName)
Dim line As String
Do
line = sr.ReadLine()
scan the line and count
Loop Until line Is Nothing
sr.Close()
Try this code, in a console application, not only could find a word
even you can get the results using a RegEx Expression.
Class TextFileInfo
Public File As System.IO.FileInfo
public Count As Integer
public FileText As String
public ItMatch as Boolean = False
Sub New (FileFullName as String,WordPattern as String)
File = new System.IO.FileInfo(FileFullName)
Using Fs As System.IO.StreamReader(File.FullName)
FileText = Fs.ReadToEnd()'//===>Read Text
End Using
Count = _CountWords(WordPattern,FileText)
ItMatch = Count > 0
End Sub
Public Sub DisplayInfo()
System.Console.WriteLine("File Name:" + File.Name)
System.Console.WriteLine("Matched Times:" & Count)
End Sub
Private Function _CountWords(Word As String,Text As String) as Integer
Dim RegEx as System.Text.RegularExpressions.Regex(Word)
return RegEx.Matches(Text).Count'//===>Returns how many times this word match in the Text
End Fuction
End Class
Public Function SearchEngine(PatternWord As String,RootDirectory As String) List(Of TextFileInfo)
Dim MatchedFiles As New List(Of TextFileInfo)
Dim RootDir As New System.IO.DirectoryInfo(RootDirectory)
For Each iTextFile as System.IO.FileInfo In RootDir.GetFiles("*.txt")
'//===>Create a object of TextFileInfo and check if the file contains the word
Dim iMatchFile as New TextFileInfo(iTextFiles.FullName,PatternWord)
If iMatchFile.ItMatch Then
'//===>Add the object to the list if it has been matches
MatchedFiles.Add(iMatchFile)
End If
Loop
retur MatchedFiles '//===>Return the results of the files that has the matched word
End Function
Sub Main()
Dim SearchResults as List(Of TextFileInfo) = SearchEngine("JajajaWord","C:\TextFiles\")
For Each iSearch As TextFileInfo In SearchResults
iSearch.DisplayInfo()
Loop
End Sub

Overwrite a specific line in a text file using VB.NET

I need to do the following:
Change the line in a text file
[Path] = "c:\this\certain\path\"
with this line
[Path] = "c:\that\other\newer\path\"
These paths will most certainly be different lengths, so I need to either replace what's in the quotes or erase the line completely and enter a new one, but in the same spot, not appended to the end of the document.
This will do the trick
Dim thefile As String = "filepath"
Dim lines() As String = System.IO.File.ReadAllLines("filepath")
lines(number of line you want to replace) = "write what you want to replace here"
System.IO.File.WriteAllLines(filepath, lines)
If you really know exactly how the line you want to replace looks and the file you're reading isn't really big, you could try to just use Replace() to add the new line instead of the old one:
Dim reader As New StreamReader("foo.txt")
Dim writer As New StreamWriter("output.txt")
Dim s = reader.ReadToEnd().Replace("[Path]: C:\oldPath\file.txt", "[Path]: C:\newPath")
writer.Write(s)
One quick way is to use readAllLines and WriteAllLines:
Dim ss() As String
ss = File.ReadAllLines([path])
ss(47) = "c:\that\other\newer\path\"
File.WriteAllLines([path], ss)
If you don't know which line to change, you can search through the array ss for it.
Read the text file into a string, iterate over each line and check if it's in the format:
[Path] = "...." (with regular expressions or simply with string.StartsWith("[Path] = "))
In this loop you should be writing out all other lines and when you are on this [Path] line, print out the modified one.
So in code (sorry, it is in C#):
var reader = File.OpenText("foo.txt");
var writer = new StreamWriter("output.txt");
string line;
while ((line=reader.ReadLine()) != null)
{
if (line.StartsWith("[Path]"))
writer.WriteLine("[Path] = \"c:\\that\\other\\newer\\path\\\"");
else
writer.WriteLine(line);
}
of course, close and dispose the StreamReader and StreamWriter.
Here's the deal: due to the way files are stored on disk, you can't write to one line without also updating every line that follows it.
There are number of ways to do this, and the one most appropriate for your situation will depend on things like the size of the file, are you doing this to a lot of files, where in the file you expect to find this, etc.
But most of the time what I like to do is actually create a copy of the old file... So as I seek through the file looking for the line(s) I need to change, I'm also writing what I've read to a new location. When I find the line, I write out the new information. I then keep seeking through the file until I reach the end at which time I close both streams, delete the original, and rename the new one.
First build a function to give the value of line 'n':
Public Function daValorConfig(ByVal numValor As Long, ByVal nomeFicheiroINI As String) As String
Dim reader As StreamReader = New StreamReader(Application.StartupPath & "\" & nomeFicheiroINI & ".ini")
Dim valor As String = ""
daValorConfig = ""
Dim i As Long = 1
Try
While i <= numValor
valor = reader.ReadLine()
i = i + 1
End While
daValorConfig = valor
reader.Close()
Catch ex As Exception
reader.Close()
MessageBox.Show(ex.Message, "Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
Err.Clear()
End Try
End Function
Then build a procedure that writes the new value on the specified line or keep the old one if the line is not the one you specify:
Public Sub guardaValorConfig(ByVal dados As String, ByVal numValor As Long, ByVal nomeFicheiroINI As String)
Dim writer As StreamWriter = New StreamWriter(Application.StartupPath & "\" & nomeFicheiroINI & ".ini")
Dim valor As String = ""
Dim i As Long = 1
Try
While i <= numValor
If i = numValor Then
writer.Write(dados)
Else
writer.Write(daValorConfig(i, nomeFicheiroINI))
End If
i = i + 1
End While
writer.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
Err.Clear()
End Try
End Sub