public static void CompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//if (checkCount != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressStream = new GZipStream(destinationStream, CompressionMode.Compress))
{
byte[] buffer = new byte[1024 * 64];
int checkCount = 0;
while ((checkCount = sourceStream.Read(buffer, 0, buffer.Length)) >= buffer.Length)
{
compressStream.Write(buffer, 0, buffer.Length);
}
compressStream.Write(buffer, 0, checkCount);
}
}
}
}
-----------------------------------------------------
CompressFile(@"E:\Test\Test.xml", @"E:\Test\Test.zip");
这是我需要压缩地址....
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//if (checkCount != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressStream = new GZipStream(destinationStream, CompressionMode.Compress))
{
byte[] buffer = new byte[1024 * 64];
int checkCount = 0;
while ((checkCount = sourceStream.Read(buffer, 0, buffer.Length)) >= buffer.Length)
{
compressStream.Write(buffer, 0, buffer.Length);
}
compressStream.Write(buffer, 0, checkCount);
}
}
}
}
-----------------------------------------------------
CompressFile(@"E:\Test\Test.xml", @"E:\Test\Test.zip");
这是我需要压缩地址....