知识大全 文件夹压缩到zip以及解压
Posted 路径
篇首语:最关情,折尽梅花,难寄相思。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 文件夹压缩到zip以及解压相关的知识,希望对你有一定的参考价值。
需要用到SharpLibZip 代码如下
Zip cs
public class Zip
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name= dirPath >压缩文件夹的路径</param>
/// <param name= fileName >生成的zip文件路径</param>
/// <param name= level >压缩级别 是存储级别 是最大压缩</param>
/// <param name= bufferSize >读取文件的缓冲区大小</param>
public void CompressDirectory(string dirPath string fileName int level int bufferSize)
byte[] buffer = new byte[bufferSize];
using (ZipOutputStream s = new ZipOutputStream(File Create(fileName)))
s SetLevel(level);
CompressDirectory(dirPath dirPath s buffer);
s Finish();
s Close();
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name= root >压缩文件夹路径</param>
/// <param name= path >压缩文件夹内当前要压缩的文件夹路径</param>
/// <param name= s ></param>
/// <param name= buffer >读取文件的缓冲区大小</param>
private void CompressDirectory(string root string path ZipOutputStream s byte[] buffer)
root = root TrimEnd( \\\\ ) + \\\\ ;
string[] fileNames = Directory GetFiles(path);
string[] dirNames = Directory GetDirectories(path);
string relativePath = path Replace(root );
if (relativePath != )
relativePath = relativePath Replace( \\\\ / ) + / ;
int sourceBytes;
foreach (string file in fileNames)
ZipEntry entry = new ZipEntry(relativePath + Path GetFileName(file));
entry DateTime = DateTime Now;
s PutNextEntry(entry);
using (FileStream fs = File OpenRead(file))
do
sourceBytes = fs Read(buffer buffer Length);
s Write(buffer sourceBytes);
while (sourceBytes > );
foreach (string dirName in dirNames)
string relativeDirPath = dirName Replace(root );
ZipEntry entry = new ZipEntry(relativeDirPath Replace( \\\\ / ) + / );
s PutNextEntry(entry);
CompressDirectory(root dirName s buffer);
/// <summary>
/// 解压缩zip文件
/// </summary>
/// <param name= zipFilePath >解压的zip文件路径</param>
/// <param name= extractPath >解压到的文件夹路径</param>
/// <param name= bufferSize >读取文件的缓冲区大小</param>
public void Extract(string zipFilePath string extractPath int bufferSize)
extractPath = extractPath TrimEnd( \\\\ ) + \\\\ ;
byte[] data = new byte[bufferSize];
int size;
using (ZipInputStream s = new ZipInputStream(File OpenRead(zipFilePath)))
ZipEntry entry;
while ((entry = s GetNextEntry()) != null)
string directoryName = Path GetDirectoryName(entry Name);
string fileName = Path GetFileName(entry Name);
//先创建目录
if (directoryName Length > )
Directory CreateDirectory(extractPath + directoryName);
if (fileName != String Empty)
using (FileStream streamWriter = File Create(extractPath + entry Name Replace( / \\\\ )))
while (true)
size = s Read(data data Length);
if (size > )
streamWriter Write(data size);
else
break;
网上找了很久 包括老外的site也找了 没找到现成的 自己写了一个
使用方法
使用方法
Zip z = new Zip();
z Extract( c:\\\\cc zip c:\\\\haha ); z Extract( cc zip c:\\\\haha ); z Extract( c:\\\\cc zip haha );
//上面的解压缩没啥大问题
z CompressDirectory( E:\\\\DotNet_Library\\\\SharpZipLib\\\\SharpZipLib_ _SourceSamples c:\\\\aa zip );
这个代码有一点点小问题 没有异常处理 压缩过程中 有文件被编辑或者被删除 那就无法压缩了
如果这样那也会异常
cha138/Article/program/net/201311/13330相关参考