Compress files...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
I need to include a way of compresing / zipping a number of document into one compressed file (basically winzip or winrar functionality) but am trying to find the best way to do this? Do I need to purchase a third party dll for this or is there a way in .net framework to achive this?

Thanks in advance

Simon
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,406
Location
Norway
Programming Experience
10+
The free SharpZipLib compression library often mentioned for this functionality, mostly because with it you can work with common compression archive file types like zip.

There is zip compatible compression support also in .Net 2.0 with System.IO.Compression namespace, where you can stream sequential data, and you can manage to read/write lots of different type data in one compressed file, but it won't work like a common zip archive file.

In .Net 3.0 there is the System.IO.Packaging namespace that produces zip compatible archives. You can use this with your VB 2005 if you install .Net 3.0 and VS2005 Workflow extensions, then reference WindowsBase (.Net tab in References dialog) and Imports System.IO.Packaging. (With VB 2008 it is already present.) Here is a basic sample that zips the calculator application:
Code:
Dim zippath As String = IO.Path.Combine(Application.StartupPath, "test.zip")
Dim zip As ZipPackage = ZipPackage.Open(zippath, IO.FileMode.Create, IO.FileAccess.ReadWrite)
Dim part As PackagePart = zip.CreatePart(New Uri("/subfolder/calc.exe", UriKind.Relative), _
                                         Net.Mime.MediaTypeNames.Application.Zip, CompressionOption.Normal)
Dim bytes() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\windows\system32\calc.exe")
part.GetStream.Write(bytes, 0, bytes.Length)
zip.Close()
With Packaging you can also create relationships to link/group data other than using folders.
 

honzas

New member
Joined
Sep 23, 2010
Messages
1
Programming Experience
10+
I've some comments to mentioned options:

  • System.IO.Packaging it is primarily intended for working with Open Packages (used e.g. in XPS or in OpenXML format for MS Office 2007 documents) and some things are hard to do using this library.
    E.g. the example above will create a ZIP file not with one file (calc.exe) within but it adds also a [Content_Types].xml file into the ZIP archive.
  • System.IO.Compression GZipStream would be used just for one stream - e.g. to compress content of one file. It does not support adding more files to the archive.
  • The SharpZipLib is broadly known and offers most of common (un)zipping features. However it is quite hard to use. For example adding a file and/or directory to a ZIP archive needs quite many lines of code - see this blogpost.

You can consider to try some another third-party product, e.g. our Rebex ZIP.

Adding file to a ZIP archive can be written in one line of code.
E.g. the previous example of adding calc.exe would be written in the following way:

Code:
Imports Rebex.IO.Compression
...
ZipArchive.Add(IO.Path.Combine(Application.StartupPath, "test.zip"), 
 "c:\windows\system32\calc.exe", "\subfolder")
You can see more samples here.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,406
Location
Norway
Programming Experience
10+
OP asked if any compression functionality was included in .Net, which is why I mentioned those two options, and explained their uses briefly, check with documentation if you need more info.
Rebex said:
Buy from $349
A price well worth mentioning ;)
SharpZipLib is broadly known and offers most of common (un)zipping features. However it is quite hard to use. For example adding a file and/or directory to a ZIP archive needs quite many lines of code
In general I would have to disagree with that. SharpZipLib also included a FastZip class from version 0.84 (released in 2005), which makes this operation a two liner.

Much later than this 2.5 years old thread I found the free DotNetZip library, which I somehow liked better than all known SharpZipLib. I posted sample compressing a folder here: http://www.vbdotnetforums.com/archiving/43745-create-zip-file-folder.html
 
Top Bottom