1 public class GZipHelper 2 { 3 public static void DecompressFile(string sourceFile, string destinationFile) 4 { 5 // make sure the source file is there 6 if (File.Exists(sourceFile) == false) 7 throw new FileNotFoundException(); 8 9 // Create the streams and byte arrays needed 10 FileStream sourceStream = null; 11 FileStream destinationStream = null; 12 GZipStream decompressedStream = null; 13 byte[] quartetBuffer = null; 14 15 try 16 { 17 // Read in the compressed source stream 18 sourceStream = new FileStream(sourceFile, FileMode.Open); 19 20 // Create a compression stream pointing to the destiantion stream 21 decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true); 22 23 // Read the footer to determine the length of the destiantion file 24 quartetBuffer = new byte[4]; 25 int position = (int)sourceStream.Length - 4; 26 sourceStream.Position = position; 27 sourceStream.Read(quartetBuffer, 0, 4); 28 sourceStream.Position = 0; 29 int checkLength = BitConverter.ToInt32(quartetBuffer, 0); 30 31 byte[] buffer = new byte[checkLength + 100]; 32 33 int offset = 0; 34 int total = 0; 35 36 // Read the compressed data into the buffer 37 while (true) 38 { 39 int bytesRead = decompressedStream.Read(buffer, offset, 100); 40 41 if (bytesRead == 0) 42 break; 43 44 offset += bytesRead; 45 total += bytesRead; 46 } 47 48 // Now write everything to the destination file 49 destinationStream = new FileStream(destinationFile, FileMode.Create); 50 destinationStream.Write(buffer, 0, total); 51 52 // and flush everyhting to clean out the buffer 53 destinationStream.Flush(); 54 } 55 catch (ApplicationException ex) 56 { 57 //MessageBox.Show(ex.Message, "解压文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error); 58 throw ex; 59 } 60 finally 61 { 62 // Make sure we allways close all streams 63 if (sourceStream != null) 64 sourceStream.Close(); 65 66 if (decompressedStream != null) 67 decompressedStream.Close(); 68 69 if (destinationStream != null) 70 destinationStream.Close(); 71 } 72 73 } 74 75 public static void CompressFile(string sourceFile, string destinationFile) 76 { 77 // make sure the source file is there 78 if (File.Exists(sourceFile) == false) 79 throw new FileNotFoundException(); 80 81 // Create the streams and byte arrays needed 82 byte[] buffer = null; 83 FileStream sourceStream = null; 84 FileStream destinationStream = null; 85 GZipStream compressedStream = null; 86 87 try 88 { 89 // Read the bytes from the source file into a byte array 90 sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); 91 92 // Read the source stream values into the buffer 93 buffer = new byte[sourceStream.Length]; 94 int checkCounter = sourceStream.Read(buffer, 0, buffer.Length); 95 96 if (checkCounter != buffer.Length) 97 { 98 throw new ApplicationException(); 99 } 100 101 // Open the FileStream to write to 102 destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write); 103 104 // Create a compression stream pointing to the destiantion stream 105 compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true); 106 107 // Now write the compressed data to the destination file 108 compressedStream.Write(buffer, 0, buffer.Length); 109 } 110 catch (ApplicationException ex) 111 { 112 //MessageBox.Show(ex.Message, "压缩文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error); 113 throw ex; 114 } 115 finally 116 { 117 // Make sure we allways close all streams 118 if (sourceStream != null) 119 sourceStream.Close(); 120 121 if (compressedStream != null) 122 compressedStream.Close(); 123 124 if (destinationStream != null) 125 destinationStream.Close(); 126 } 127 } 128 }