/// <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")); 可以
/// <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")); 可以
/// <summary>
/// 存放待压缩的文件的绝对路径
/// </summary>
private List<string> AbsolutePaths { set; get; }
2、ZipOutputStream ComStream = new ZipOutputStream(File.Create("F://新建文件夹"));
为什么会报错????
我刚试过,并没有这个问题
/// 解压
/// </summary>
/// <param name="_depositPath">压缩文件路径</param>
/// <param name="_floderPath">解压的路径</param>
/// <returns></returns>
public static bool DeCompressionZip(string _depositPath, string _floderPath)
{
bool result = true;
FileStream fs = null;
try
{
ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath)); ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件
Directory.CreateDirectory(_floderPath);//创建解压文件夹
while (ze != null)//如果解压完ze则是null
{
if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名
{
string[] strs = ze.Name.Split('\\');//如果文件名中包含’\\‘则表明有文件夹
if (strs.Length > 1)
{
//两层循环用于一层一层创建文件夹
for (int i = 0; i < strs.Length - 1; i++)
{
string floderPath = _floderPath;
for (int j = 0; j < i; j++)
{
floderPath = floderPath + "\\" + strs[j];
}
floderPath = floderPath + "\\" + strs[i];
Directory.CreateDirectory(floderPath);
}
}
string path = ze.Name.Substring(ze.Name.IndexOf('/') + 1);
fs = new FileStream(_floderPath + "\\" + path, FileMode.OpenOrCreate, FileAccess.Write);//创建文件
//循环读取文件到文件流中
while (true)
{
byte[] bts = new byte[1024];
int i = InpStream.Read(bts, 0, bts.Length);
if (i > 0)
{
fs.Write(bts, 0, i);
}
else
{
fs.Flush();
fs.Close();
break;
}
}
}
ze = InpStream.GetNextEntry();
}
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
string errorMsg = ex.Message;
result = false;
}
return result;
} /// <summary>
/// 压缩目录
/// </summary>
/// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
/// <returns></returns>
public static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
{
bool res;
res = Directory.Exists(FolderToZip);
if (!Directory.Exists(FolderToZip))
{
return false;
} ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
s.SetLevel(6);
s.Password = Password; res = ZipFileDictory(FolderToZip, s, ""); s.Finish();
s.Close(); return res;
} /// <summary>
/// 递归压缩文件夹方法
/// </summary>
/// <param name="FolderToZip"></param>
/// <param name="s"></param>
/// <param name="ParentFolderName"></param>
public static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
{
bool res = true;
string[] folders, filenames;
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32(); try
{ //创建当前文件夹
//entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
//s.PutNextEntry(entry);
//s.Flush();
//先压缩文件,再递归压缩文件夹
filenames = Directory.GetFiles(FolderToZip);
foreach (string file in filenames)
{
//打开压缩文件
fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(file))); entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close(); crc.Reset();
crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect(1);
}
folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{
if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
{
return false;
}
} return res;
}
压缩和解压缩zip方法 完全没问题。