How to compress and decompress file
来源:互联网 发布:淘宝优惠券赚佣金平台 编辑:程序博客网 时间:2024/05/19 13:45
using System.IO;
using System.IO.Compression;
using System.Text;
Here are sample functions to compress and decompress a file:
private void TestCompress()
{
string srcFile = "C://temp//file-to-compress.txt";
string dstFile = "C://temp//compressed-file.gzip";
FileStream fsIn = null; // will open and read the srcFile
FileStream fsOut = null; // will be used by the GZipStream for output to the dstFile
GZipStream gzip = null;
byte[] buffer;
int count = 0;
try
{
fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsOut, CompressionMode.Compress, true);
fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[fsIn.Length];
count = fsIn.Read(buffer, 0, buffer.Length);
fsIn.Close();
fsIn = null;
// compress to the destination file
gzip.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
// handle or display the error
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
{
gzip.Close();
gzip = null;
}
if (fsOut != null)
{
fsOut.Close();
fsOut = null;
}
if (fsIn != null)
{
fsIn.Close();
fsIn = null;
}
}
}
private void TestDecompress()
{
string srcFile = "C://temp//compressed-file.gzip";
string dstFile = "C://temp//decompressed-file.txt";
FileStream fsIn = null; // will open and read the srcFile
FileStream fsOut = null; // will be used by the GZipStream for output to the dstFile
GZipStream gzip = null;
const int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int count = 0;
try
{
fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
while (true)
{
count = gzip.Read(buffer, 0, bufferSize);
if (count != 0)
{
fsOut.Write(buffer, 0, count);
}
if (count != bufferSize)
{
// have reached the end
break;
}
}
}
catch (Exception ex)
{
// handle or display the error
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
{
gzip.Close();
gzip = null;
}
if (fsOut != null)
{
fsOut.Close();
fsOut = null;
}
if (fsIn != null)
{
fsIn.Close();
fsIn = null;
}
}
}
- How to compress and decompress file
- How to decompress a .bz2 file 解压
- how to compress viewstate
- Linux compress decompress
- kernel: How to decompress the zImage?
- How to find code and file
- how to use Properties and File to Save Data
- how to mount and unmount ISO file in Ubuntu
- How to Open "File And Replace" Dialog Box ?
- how to extract and convert audio file formats
- How to open and sort a CSV file with Ruby
- BMFont and how to interpret the .fnt file
- how to create image file and format it
- how to access and operate a binarry file ?
- How to read and parse CSV file in Java
- QT27 how to read text file and display file to a textbrowser or textedit
- Asset file how to
- Win8 压缩Compress与解压Decompress函数实例
- 修改本地hosts表用域名访问本地站点
- 常用Sql语句
- UpdatePanel中使用脚本文件(ScriptManager.RegisterStartupScript 方法)
- 做人做事啊···小故事看大道理
- C#中的partial
- How to compress and decompress file
- jsp servlet中的过滤器Filter配置总结
- 行列转换1
- 数据库中的系统函数介绍!
- hdu 1866 A + B forever!
- PowerDesigner
- 向android模拟器中复制文件报out of memory错误解决
- 基于实验开发板的按键处理与LED显示
- 常见bug:函数返回引用/指针