PDA

View Full Version : کد: Zip و UnZip کردن فایل



Amir Taghavi
سه شنبه 03 بهمن 1385, 12:45 عصر
سلام

بعضی از دوستان بدنبال کامپوننت Zip و UnZip کردن فایل ها بودند؛ غافل از ینکه کد در System.IO.Compression و ما گرد کامپوننت میگردیم.!!!


Imports System.IO
Imports System.IO.Compression

Namespace ECP

Public Class ZipUtility

Public Sub CompressFile(ByVal sourceFile As String, ByVal destinationFile As String)

' make sure the source file is there
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException
End If

' Create the streams and byte arrays needed
Dim buffer As Byte() = Nothing
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing

Try
' Read the bytes from the source file into a byte array
sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)

' Read the source stream values into the buffer
buffer = New Byte(sourceStream.Length) {}
Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)

' Open the FileStream to write to
destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)

' Create a compression stream pointing to the destiantion stream
compressedStream = New GZipStream(destinationStream, CompressionMode.Compress, True)

'Now write the compressed data to the destination file
compressedStream.Write(buffer, 0, buffer.Length)

Catch ex As ApplicationException
MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Make sure we allways close all streams
If sourceStream IsNot Nothing Then
sourceStream.Close()
End If
If compressedStream IsNot Nothing Then
compressedStream.Close()
End If
If destinationStream IsNot Nothing Then
destinationStream.Close()
End If
End Try

End Sub

Public Sub DecompressFile(ByVal sourceFile As String, ByVal destinationFile As String)

' make sure the source file is there
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException
End If

' Create the streams and byte arrays needed
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim decompressedStream As GZipStream = Nothing
Dim quartetBuffer As Byte() = Nothing

Try
' Read in the compressed source stream
sourceStream = New FileStream(sourceFile, FileMode.Open)

' Create a compression stream pointing to the destiantion stream
decompressedStream = New GZipStream(sourceStream, CompressionMode.Decompress, True)

' Read the footer to determine the length of the destiantion file
quartetBuffer = New Byte(4) {}
Dim position As Integer = CType(sourceStream.Length, Integer) - 4
sourceStream.Position = position
sourceStream.Read(quartetBuffer, 0, 4)
sourceStream.Position = 0
Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)

Dim buffer(checkLength + 100) As Byte
Dim offset As Integer = 0
Dim total As Integer = 0

' Read the compressed data into the buffer
While True
Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)
If bytesRead = 0 Then
Exit While
End If
offset += bytesRead
total += bytesRead
End While

' Now write everything to the destination file
destinationStream = New FileStream(destinationFile, FileMode.Create)
destinationStream.Write(buffer, 0, total)

' and flush everyhting to clean out the buffer
destinationStream.Flush()

Catch ex As ApplicationException
MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Make sure we allways close all streams
If sourceStream IsNot Nothing Then
sourceStream.Close()
End If
If decompressedStream IsNot Nothing Then
decompressedStream.Close()
End If
If destinationStream IsNot Nothing Then
destinationStream.Close()
End If
End Try

End Sub
End Class
End Namespace

Sub Zero
سه شنبه 03 بهمن 1385, 14:27 عصر
امیر جان تو که این همه زحمت کشیدی لا اقل یه نمونه کامل (سورس همراه با برنامه ) درست می کردی توی تاپیک VB 2005 Tips (http://www.barnamenevis.org/forum/showthread.php?p=301175#post301175) میزاشتی تا همه استفاده کنند .

linux
سه شنبه 03 بهمن 1385, 14:58 عصر
شما با استفاده از این کد نمی توانید چندتا فایل را زیب کرده و در یک فایل بریزید.

Amir Taghavi
سه شنبه 03 بهمن 1385, 15:21 عصر
شما با استفاده از این کد نمی توانید چندتا فایل را زیب کرده و در یک فایل بریزید.

با کمی تغییر حتما" امکان داره. کار نشد نداره!!!

hesam_a110
دوشنبه 14 آبان 1386, 14:52 عصر
اگه ممکنه کد مربوط به چند تا فایل را بزاره ممنون می شم یا حتی یک پوشه

نیک باشید

amirsajjadi
دوشنبه 14 آبان 1386, 22:18 عصر
اینم برنامه ای که از خود سایت مایکروسافت دانلود کردم برای فشرده سازی یک یا چند تا فایل است

Alirezanet
چهارشنبه 19 اسفند 1388, 20:36 عصر
اینم برنامه ای که از خود سایت مایکروسافت دانلود کردم برای فشرده سازی یک یا چند تا فایل است

برنامه ای که ضمیمه کردین توی .net2008 درست کار نمیکنه ولی ممنون ...
مثال دیگه ای کسی میتونه بزنه ؟!
یا نمونه کد بده .. .
من توی برنامم یه فولدر با چند فایل دارم میخوام به این روش ازشون Backup بگیرم و بتونم بعدا دوباره به برنامه اضافشون کنم ...

ramin2nt2
سه شنبه 25 اسفند 1388, 15:18 عصر
سلام
این لینک رو ببینید :

http://www.codeplex.com/DotNetZip

Alirezanet
سه شنبه 25 اسفند 1388, 15:46 عصر
سلام
این لینک رو ببینید :

http://www.codeplex.com/DotNetZip

سلام ممنون ولی لینکشو گذاشته بودم اینجا یادم رفت اضافه کنم ...
اینجا کامل توضیح دادم :
http://barnamenevis.org/forum/showthread.php?t=210178