System
注意
System
(downmoon原作)
類 說明
DeflateStream 提供用於使用 Deflate 算法壓縮和解壓縮流的方法和屬性
GZipStream 提供用於壓縮和解壓縮流的方法和屬性
枚舉 說明
CompressionMode 指定是否壓縮或解壓縮基礎流
下面以 GZipStream 為例說明
注意
提供用於壓縮和解壓縮流的方法和屬性
命名空間:System
程序集:System(在 system
語法
Visual Basic(聲明)
Public Class GZipStream
Inherits Stream
Visual Basic(用法)
Dim instance As GZipStream
C#
public class GZipStream : Stream
C++
public ref class GZipStream : public Stream
J#
public class GZipStream extends Stream
JScript
public class GZipStream extends Stream
備注
此類表示 GZip 數據格式
給繼承者的說明 當從 GZipStream 繼承時
下面提供 一個完整的壓縮與解壓類(downmoon原作 )
class clsZip
public void CompressFile ( string sourceFile
// make sure the source file is there
if ( File
throw new FileNotFoundException ( );
// Create the streams and byte arrays needed
byte[] buffer = null;
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream compressedStream = null;
try
// Read the bytes from the source file into a byte array
sourceStream = new FileStream ( sourceFile
// Read the source stream values into the buffer
buffer = new byte[sourceStream
int checkCounter = sourceStream
if ( checkCounter != buffer
throw new ApplicationException ( );
}
// Open the FileStream to write to
destinationStream = new FileStream ( destinationFile
// Create a compression stream pointing to the destiantion stream
compressedStream = new GZipStream ( destinationStream
// Now write the compressed data to the destination file
compressedStream
}
catch ( ApplicationException ex )
MessageBox
}
finally
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream
if ( compressedStream != null )
compressedStream
if ( destinationStream != null )
destinationStream
}
}
public void DecompressFile ( string sourceFile
// make sure the source file is there
if ( File
throw new FileNotFoundException ( );
// Create the streams and byte arrays needed
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream decompressedStream = null;
byte[] quartetBuffer = null;
try
// Read in the compressed source stream
sourceStream = new FileStream ( sourceFile
// Create a compression stream pointing to the destiantion stream
decompressedStream = new GZipStream ( sourceStream
// Read the footer to determine the length of the destiantion file
quartetBuffer = new byte[
int position = (int)sourceStream
sourceStream
sourceStream
sourceStream
int checkLength = BitConverter
byte[] buffer = new byte[checkLength +
int offset =
int total =
// Read the compressed data into the buffer
while ( true )
int bytesRead = decompressedStream
if ( bytesRead ==
break;
offset += bytesRead;
total += bytesRead;
}
// Now write everything to the destination file
destinationStream = new FileStream ( destinationFile
destinationStream
// and flush everyhting to clean out the buffer
destinationStream
}
catch ( ApplicationException ex )
MessageBox
}
finally
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream
if ( decompressedStream != null )
decompressedStream
if ( destinationStream != null )
destinationStream
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12971.html