Zipping files

  • Thread starter Thread starter John
  • Start date Start date
VB.NET has the GZIPStream class. The method below compresses the IN bytes,
returning the OUT bytes. Note this method is hacked from another method I
have, so I didn't test it. Reversing the operation is also simple, but this
should give you the idea. I think programs like WinZip can read GZip files.
From MSDN:

"This class
represents the gzip data format, which uses an industry standard algorithm
for lossless file compression and decompression. The format includes a
cyclic redundancy check value for detecting data corruption. The gzip data
format uses the same algorithm as the DeflateStream class, but can be
extended to use other compression formats. The format can be readily
implemented in a manner not covered by patents. The format for gzip is
available from the RFC 1952, "GZIP ." This class cannot be used to compress
files larger than 4 GB."



Imports System.IO
Imports System.IO.Compression

Public Sub Compress(ByVal inBytes() As Byte, ByRef outBytes() As Byte)

Dim theGZIPStream As GZipStream = Nothing


If inBytes.Length < 1 Then
Throw New ArgumentException("inBytes.length < 1")
End If


Using theMemoryStream As New MemoryStream()

theGZIPStream = New GZipStream(theMemoryStream,
CompressionMode.Compress)
theGZIPStream.Write(m_Bytes, 0, inBytes.Length)

outBytes = theMemoryStream.ToArray()

End Using

End Sub
 
John said:
Is there a way in vb.net to zip files?

..NET System.IO.Compression and zip files
<URL:http://blogs.msdn.com/dotnetinterop/archive/2006/04/05/567402.aspx>

Using GZipStream for Compression in .NET [Brian Grunkemeyer]
<URL:http://blogs.msdn.com/bclteam/archive/2005/06/15/429542.aspx>

ZipPackage Class
<URL:http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx>

Compressing files and data
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=compression&lang=en>
 
I guess, files zipped with gzipstream can be decompressed by winzip but reverse is not true.
And gzipstream few limitations to zip multiple files at a time, which winzip does.
 
Back
Top