zameer21
asked on
How to zip files in a folder using Vb.net 2005
I need to zip files in a folder using Vb.net,how can i do it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi have tried also to use that namespace (System.IO.Compression) but I found complicated and not easy to handle. I still prefer SharpZipLib for my apps.
Indeed GZipStream or DeflateStream alone won't allow you to create valid Zip files.
.net 2.0 doesn't have a direct facility to do this. .net 3.0 instead gives you the ZipPackage class in the new System.IO.Packaging namespace.
(http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx)
But you can use a library like DotNetZip, as previously suggested, or, if you don't want 3rd party libraries, you can use Windows shell facilities as explained here:
http://www.codeproject.com/KB/cs/compresswithwinshellapics.aspx
.net 2.0 doesn't have a direct facility to do this. .net 3.0 instead gives you the ZipPackage class in the new System.IO.Packaging namespace.
(http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx)
But you can use a library like DotNetZip, as previously suggested, or, if you don't want 3rd party libraries, you can use Windows shell facilities as explained here:
http://www.codeproject.com/KB/cs/compresswithwinshellapics.aspx
ASKER
I really like SharpZipLib,but still i would like to write my own code if i could
>> I really like SharpZipLib,but still i would like to write my own code if i could
Don't understand because you have to write you code. Here's an example of SharpZipLib, that uses a progress bar and writes to a log file:
Don't understand because you have to write you code. Here's an example of SharpZipLib, that uses a progress bar and writes to a log file:
Private Sub ZipIt()
ZipProgressBar.Maximum = lvFicheiros.Items.Count
ZipProgressBar.Value = 0
ZipProgressBar.Step = 1
ZipProgressBar.Visible = True
lblBackup.Visible = True
Application.DoEvents()
Try
Dim strmZipOutputStream As ZipOutputStream
Dim outputname As String = DateTime.Now.ToString("yyyyMMdd-HHmm") & ".zip"
Directory.SetCurrentDirectory(My.Settings.OutputDir)
strmZipOutputStream = New ZipOutputStream(File.Create(outputname))
' Compression Level: 0-9
strmZipOutputStream.SetLevel(9)
For Each item As ListViewItem In lvFicheiros.Items
Dim strmFile As FileStream
If TryOpenFile(item.SubItems(1).Text) Then
strmFile = File.OpenRead(item.SubItems(1).Text)
Else
Dim fl As New FileInfo(item.SubItems(1).Text)
File.Copy(item.SubItems(1).Text, Environment.CurrentDirectory & "\" & fl.Name, True)
strmFile = File.OpenRead(Environment.CurrentDirectory & "\" & fl.Name)
FilesToDelete.Add(Environment.CurrentDirectory & "\" & fl.Name)
fl = Nothing
End If
Dim abyBuffer(CInt(strmFile.Length - 1)) As Byte
strmFile.Read(abyBuffer, 0, abyBuffer.Length)
Dim objZipEntry As ZipEntry = New ZipEntry(item.SubItems(0).Text)
objZipEntry.DateTime = DateTime.Now
objZipEntry.Size = strmFile.Length
strmFile.Close()
strmZipOutputStream.PutNextEntry(objZipEntry)
strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)
ZipProgressBar.PerformStep()
ZipProgressBar.Update()
Application.DoEvents()
Next
strmZipOutputStream.Finish()
strmZipOutputStream.Close()
Dim outputstring As String = Now.ToString("yyyy/MM/dd HH:mm:ss") & " - Backup name: " & outputname
My.Computer.FileSystem.WriteAllText(Environment.CurrentDirectory & "\autobackup.log", outputstring & vbNewLine, True)
lvLog.Items.Add(outputstring)
lvLog.EnsureVisible(lvLog.Items.Count - 1)
Catch ex As Exception
MsgBox("Erro message:" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical)
Finally
ZipProgressBar.Visible = False
lblBackup.Visible = False
End Try
End Sub
I think what he means is that he would like to have all of the code in his project and not in a third-party library. I will say that since SharpZipLib is open source you COULD take the code and incorporate it into your software, just remember that you have to live by the open source license rules.
The page I linked to earlier has a source example of how to use System.IO.Compression to create a Zip class that is quite easy to use once you've got the base class working. http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx
Your choice.
The page I linked to earlier has a source example of how to use System.IO.Compression to create a Zip class that is quite easy to use once you've got the base class working. http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx
Your choice.
ASKER
Hi JPaulino,
Here is something i found.
Private Sub Zip()
' Zip up the files - From SharpZipLib Demo Code
Dim s As ZipOutputStream = New ZipOutputStream(System.IO. File.Creat e("C:\test file2.zip" ))
s.SetLevel(9) ' 0-9, 9 being the highest level of compression
Dim buffer() As Byte
ReDim buffer(4096)
Dim filenames As String() = Directory.GetFiles("C:\Tes t\in")
Dim f As String
For Each f In filenames
Dim entry As ZipEntry = New ZipEntry(Path.GetFileName( f))
entry.DateTime = DateTime.Now
s.PutNextEntry(entry)
Dim fs As FileStream = System.IO.File.OpenRead(f)
Dim sourceBytes As Integer = 1
Do Until (sourceBytes <= 0)
sourceBytes = fs.Read(buffer, 0, buffer.Length)
s.Write(buffer, 0, sourceBytes)
Loop
fs.Close()
Next
' clean up
s.Finish()
s.Close()
End Sub
this works and is pretty simple.I will give you all points for the SharpZipLibraray, though i was looking for code.Thanks......
Here is something i found.
Private Sub Zip()
' Zip up the files - From SharpZipLib Demo Code
Dim s As ZipOutputStream = New ZipOutputStream(System.IO.
s.SetLevel(9) ' 0-9, 9 being the highest level of compression
Dim buffer() As Byte
ReDim buffer(4096)
Dim filenames As String() = Directory.GetFiles("C:\Tes
Dim f As String
For Each f In filenames
Dim entry As ZipEntry = New ZipEntry(Path.GetFileName(
entry.DateTime = DateTime.Now
s.PutNextEntry(entry)
Dim fs As FileStream = System.IO.File.OpenRead(f)
Dim sourceBytes As Integer = 1
Do Until (sourceBytes <= 0)
sourceBytes = fs.Read(buffer, 0, buffer.Length)
s.Write(buffer, 0, sourceBytes)
Loop
fs.Close()
Next
' clean up
s.Finish()
s.Close()
End Sub
this works and is pretty simple.I will give you all points for the SharpZipLibraray, though i was looking for code.Thanks......
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Missing somethig:
Private FilesToDelete As New ArrayList
and call it this way:
Call ZipIt()
Call DeleteFiles()
Private FilesToDelete As New ArrayList
and call it this way:
Call ZipIt()
Call DeleteFiles()
Visit this website for a code example of how to do this:
http://blogs.msdn.com/dotnetinterop/archive/2006/04/05/.NET-System.IO.Compression-and-zip-files.aspx