读书人

解压缩解决办法

发布时间: 2013-08-25 10:49:56 作者: rapoo

解压缩

/// <summary>          /// 压缩文件或者文件夹          /// </summary>          
/// <param name="_depositPath">压缩后文件的存放路径 如C:\\windows\abc.zip</param>
/// <returns></returns>
public bool CompressionZip(string _depositPath)
{
bool result = true;
FileStream fs = null;
try
{
ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));
ComStream.SetLevel(9); //压缩等级
foreach (string path in AbsolutePaths)
{
//如果是目录
if (Directory.Exists(path))


{
ZipFloder(path, ComStream, path);
}
else if (File.Exists(path))//如果是文件
{
fs = File.OpenRead(path);
byte[] bts = new byte[fs.Length];
fs.Read(bts, 0, bts.Length);
ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
ComStream.PutNextEntry(ze); //为压缩文件流提供一个容器
ComStream.Write(bts, 0, bts.Length); //写入字节
} }


ComStream.Finish(); // 结束压缩
ComStream.Close();
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
errorMsg = ex.Message;
result = false;
}
return result;
}
//压缩文件夹
private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
{
foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())


{
if (Directory.Exists(item.FullName))
{
ZipFloder(_OfloderPath, zos, item.FullName);
}
else if (File.Exists(item.FullName))//如果是文件
{
DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
string fullName2 = new FileInfo(item.FullName).FullName;
string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录
FileStream fs = File.OpenRead(fullName2);
byte[] bts = new byte[fs.Length];
fs.Read(bts, 0, bts.Length);


ZipEntry ze = new ZipEntry(path);
zos.PutNextEntry(ze); //为压缩文件流提供一个容器
zos.Write(bts, 0, bts.Length); //写入字节
}
}
}



http://developer.51cto.com/art/201212/374573.htm
网上找到压缩方法。
但是我想问AbsolutePaths是什么?
ZipOutputStream ComStream = new ZipOutputStream(File.Create("F://新建文件夹"));
为什么会报错????
ZipOutputStream ComStream = new ZipOutputStream(File.Create("F://新建文件夹.zip")); 可以


[解决办法]
1、源代码文件里面有注释:
/// <summary>
/// 存放待压缩的文件的绝对路径
/// </summary>
private List<string> AbsolutePaths { set; get; }
2、ZipOutputStream ComStream = new ZipOutputStream(File.Create("F://新建文件夹"));
为什么会报错????
我刚试过,并没有这个问题

读书人网 >C#

热点推荐