Вот нашел пример класса реализующий сжатие файла, мне этот пример понравился. Я добавил пример работы с классом по архивированию (разархивацию не делал, но она делается анологично), работу класса проверял путем разорхивации WinRAR-ом, все работает.
Класс:
- Код: Выделить всё
Imports System.IO ' for Stream object
Imports System.IO.Compression ' for DeflateStream and GZipStream
Public Enum eCompressMethod As Byte
Deflate = 1
Gzip = 2
End Enum
Public Class ZIPCompress
Const CHUNKSIZE As Integer = 32768
Public Shared Sub Compress(ByVal input As Stream, ByVal output As Stream, ByVal CompressMethod As eCompressMethod, ByVal progress As Object, ByVal maxprogress As Integer)
Dim zipStream As Stream = Nothing 'The zip stream we will use for compression
Try
Select Case CompressMethod
Case eCompressMethod.Deflate
zipStream = New DeflateStream(output, CompressionMode.Compress)
Case eCompressMethod.Gzip
zipStream = New GZipStream(output, CompressionMode.Compress)
End Select
'Compress the original bytes array
CopyStream(input, zipStream, progress, maxprogress)
Catch ex As Exception
Throw ex
Finally
If Not zipStream Is Nothing Then
zipStream.Close()
zipStream = Nothing
End If
End Try
End Sub
Public Shared Sub Decompress(ByVal input As Stream, ByVal output As Stream, ByVal CompressMethod As eCompressMethod, ByVal progress As Object, ByVal maxprogress As Integer)
Dim zipStream As Stream = Nothing 'The zip stream we will use for decompression
Try
Select Case CompressMethod
Case eCompressMethod.Deflate
zipStream = New DeflateStream(input, CompressionMode.Decompress)
Case eCompressMethod.Gzip
zipStream = New GZipStream(input, CompressionMode.Decompress)
End Select
'DeCompress
CopyStream(zipStream, output, progress, maxprogress)
Catch ex As Exception
Throw ex
Finally
If Not zipStream Is Nothing Then
zipStream.Close()
zipStream = Nothing
End If
End Try
End Sub
Private Shared Sub CopyStream(ByVal input As Stream, ByVal output As Stream, ByVal progress As Object, ByVal maxprogress As Integer)
progress.maximum = maxprogress
Dim bytes(CHUNKSIZE) As Byte
Dim n As Integer
While True
n = input.Read(bytes, 0, bytes.Length)
If n = 0 Then Exit Sub
output.Write(bytes, 0, n)
If progress.value + bytes.Length > maxprogress Then progress.value = maxprogress Else progress.value = progress.value + bytes.Length
End While
End Sub
End Class
Код формы: (на форме две кнопки, прогресс,Open диалог и два RadioButton для выбора метода)
- Код: Выделить всё
Public infile As String
Public outfile As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim met As Byte
infile = OpenFileDialog1.FileName
If RadioButton1.Checked = True Then
met = eCompressMethod.Gzip
outfile = infile & ".gzip"
else
met = eCompressMethod.Deflate
outfile = infile & ".def"
End If
Dim infs As New System.IO.FileStream(infile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Dim outfs As New System.IO.FileStream(outfile, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Write)
ProgressBar1.Value = 0
ZIPCompress.Compress(infs, outfs, met, ProgressBar1, infs.Length)
outfs.Close()
infs.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Может кому-то пригодится.