C#压缩与解压文件源码
2008-03-25 17:57
471 查看
//获得压缩的文件夹
//压缩文件夹的名字
string name = file + ".rar";
//压缩文件的流对象
// MessageBox.Show(name);
ZipOutputStream output = new ZipOutputStream(File.Create(name));
output.SetLevel(6);
string[] dir = Directory.GetFiles(file);
//存放文件数据
Crc32 crc = new Crc32();
foreach (string myFile in dir)
{
FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
//存储要压缩的文件
ZipEntry entry = new ZipEntry(myFile);
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
fs.Close();
crc.Reset(); //清除crc内容
crc.Update(bt); //更新文件内容到crc中
entry.Crc = crc.Value; //将文件内容放到压缩文件中
output.PutNextEntry(entry);
//将数据写入压缩流中
output.Write(bt, 0, bt.Length);
}
output.Close();
MessageBox.Show("压缩成功");
解压文件
string file = this.txtUZip.Text;
//读压缩文件内容流
ZipInputStream input = new ZipInputStream(File.OpenRead(file));
ZipEntry entry = input.GetNextEntry();
//要存放的路径
string dir = this.txtPath.Text;
while (entry != null)
{
string filename = entry.Name;
//读压缩文件内容
int size = 2048;
byte[] bt = new byte[size];
FileStream inpuFs = File.Create(dir+filename.Substring(filename.LastIndexOf("//")));
while (true)
{
size = input.Read(bt, 0, bt.Length);
if (size > 0)
{
inpuFs.Write(bt, 0, size);
}
else
{
inpuFs.Close();
break;
}
}
entry = input.GetNextEntry();
}
input.Close();
源码下载
http://download.csdn.net/source/394595
//压缩文件夹的名字
string name = file + ".rar";
//压缩文件的流对象
// MessageBox.Show(name);
ZipOutputStream output = new ZipOutputStream(File.Create(name));
output.SetLevel(6);
string[] dir = Directory.GetFiles(file);
//存放文件数据
Crc32 crc = new Crc32();
foreach (string myFile in dir)
{
FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
//存储要压缩的文件
ZipEntry entry = new ZipEntry(myFile);
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
fs.Close();
crc.Reset(); //清除crc内容
crc.Update(bt); //更新文件内容到crc中
entry.Crc = crc.Value; //将文件内容放到压缩文件中
output.PutNextEntry(entry);
//将数据写入压缩流中
output.Write(bt, 0, bt.Length);
}
output.Close();
MessageBox.Show("压缩成功");
解压文件
string file = this.txtUZip.Text;
//读压缩文件内容流
ZipInputStream input = new ZipInputStream(File.OpenRead(file));
ZipEntry entry = input.GetNextEntry();
//要存放的路径
string dir = this.txtPath.Text;
while (entry != null)
{
string filename = entry.Name;
//读压缩文件内容
int size = 2048;
byte[] bt = new byte[size];
FileStream inpuFs = File.Create(dir+filename.Substring(filename.LastIndexOf("//")));
while (true)
{
size = input.Read(bt, 0, bt.Length);
if (size > 0)
{
inpuFs.Write(bt, 0, size);
}
else
{
inpuFs.Close();
break;
}
}
entry = input.GetNextEntry();
}
input.Close();
源码下载
http://download.csdn.net/source/394595
相关文章推荐
- C# 解压及压缩文件源码
- c#解压,压缩文件!!!
- C#解压或压缩文件夹
- C#中关于zip压缩解压帮助类的封装 附源码下载
- 使用 C# 对文件进行压缩和解压
- C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能
- 使用C#进行文件压缩和解压
- 【转载】使用C#2.0进行文件压缩和解压
- c#实现metro文件压缩解压示例
- 使用C#2.0进行文件压缩和解压
- c# 压缩文件、压缩多级目录及解压
- c#压缩文件,解压文件写法
- C#第三方zip解压压缩工具,带事例源码
- C#实现从FTP服务器下载压缩文件并解压.
- C#压缩解压zip 文件
- 通过C#代码 压缩/解压文件
- 用C#调用Windows API压缩,解压文件
- C# 解压压缩文件类(ZIP格式)
- (转载)C#利用ICSharpCode.SharpZipLib.dll压缩文件和解压文件
- c# 压缩解压文件