1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
Public Shared Sub Main(ByVal args As String())
Try
CompressFile("Test.txt", "Test.gz")
Console.WriteLine("Fichier Test.txt compressé vers Test.gz")
DecompressFile("Test.gz", "TestDecompress.txt")
Console.WriteLine("Fichier Test.gz décompressé vers TestDecompress.txt")
Catch ex As Exception
Console.WriteLine(String.Format("{0}: {1}", ex.[GetType](), ex.Message))
End Try
Console.ReadLine()
End Sub
Public Shared Sub CompressFile(ByVal NomFichierACompresser As String, ByVal NomFichierCompresse As String)
Dim outCompressedStream As GZipStream = Nothing
Dim inFileStream As FileStream = Nothing
Dim outFileStream As FileStream = Nothing
Dim inBuffer As Byte()
Try
' Ouvrir et lire le fichier à compresser
inFileStream = New FileStream(NomFichierACompresser, FileMode.Open, FileAccess.Read, FileShare.Read)
inBuffer = New Byte(Convert.ToInt32(inFileStream.Length)) {}
inFileStream.Read(inBuffer, 0, inBuffer.Length)
' Ouverture et creation du fichier à compresser
outFileStream = New FileStream(NomFichierCompresse, FileMode.CreateNew, FileAccess.Write, FileShare.Read)
' Ouverture du flux de compression
outCompressedStream = New GZipStream(outFileStream, CompressionMode.Compress)
' Compression
outCompressedStream.Write(inBuffer, 0, inBuffer.Length)
Finally
' Fermeture des fluxs
If inFileStream IsNot Nothing Then
inFileStream.Close()
End If
If outCompressedStream IsNot Nothing Then
outCompressedStream.Close()
End If
If outFileStream IsNot Nothing Then
outFileStream.Close()
End If
End Try
End Sub
Public Shared Sub DecompressFile(ByVal sArchiveFile As String, ByVal sDecompressedFile As String)
Dim inDecompressedStream As GZipStream = Nothing
Dim inFileStream As FileStream = Nothing
Dim outFileStream As FileStream = Nothing
Dim inBuffer As Byte()
Dim iInBufferDataLength As Integer
Dim fiArchiveFile As FileInfo
Try
' Ouverture du fichier à decompresser
inFileStream = New FileStream(sArchiveFile, FileMode.Open, FileAccess.Read, FileShare.Read)
' Creation du flux de decompression
inDecompressedStream = New GZipStream(inFileStream, CompressionMode.Decompress)
'
fiArchiveFile = New FileInfo(sArchiveFile)
inBuffer = New Byte(Convert.ToInt32(fiArchiveFile.Length) * 20) {}
' La decompression se fait en lisant le flux GzipStream
' On utilise cette fonction maison car Stream.Length n'est
' pas disponible pour GzipStream. A la sortie de cette fonction, inBuffer
' contient les données non compressées
iInBufferDataLength = ReadAllBytesFromStream(inDecompressedStream, inBuffer)
' Creation du fichier decompressé
outFileStream = New FileStream(sDecompressedFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read)
outFileStream.Write(inBuffer, 0, iInBufferDataLength)
Finally
' Fermeture flux ouvers
If inFileStream IsNot Nothing Then
inFileStream.Close()
End If
If inDecompressedStream IsNot Nothing Then
inDecompressedStream.Close()
End If
If outFileStream IsNot Nothing Then
outFileStream.Close()
End If
End Try
End Sub
Private Shared Function ReadAllBytesFromStream(ByVal stream As GZipStream, ByRef buffer As Byte()) As Integer
Dim iOffset As Integer = 0
Dim iTotalBytesRead As Integer = 0
Dim iBytesRead As Integer = 0
' Le buffer doit au moins avoir 4ko en taille
If buffer.Length < 4096 Then
Array.Resize(Of Byte)(buffer, 4096)
End If
' La decompression se fait en lisant le flux GzipStream
' On utilise cette fonction maison car Stream.Length n'est
' pas disponible pour GzipStream
While True
iBytesRead = stream.Read(buffer, iOffset, 4096)
If iBytesRead = 0 Then
Exit While
End If
iOffset += iBytesRead
iTotalBytesRead += iBytesRead
If buffer.Length < (iOffset + 4096) Then
Array.Resize(Of Byte)(buffer, 2 * buffer.Length)
End If
End While
Return iTotalBytesRead
End Function |
Partager