需要用到SharpLibZip代码如下 Zipcs 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 dirPathstring fileNameint levelint bufferSize) { byte[] buffer = new byte[bufferSize]; using (ZipOutputStream s = new ZipOutputStream(FileCreate(fileName))) { sSetLevel(level); CompressDirectory(dirPath dirPath s buffer); sFinish(); sClose(); } } /// <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 = rootTrimEnd(\\) + \\; string[] fileNames = DirectoryGetFiles(path); string[] dirNames = DirectoryGetDirectories(path); string relativePath = pathReplace(root ); if (relativePath != ) { relativePath = relativePathReplace(\\ /) + /; } int sourceBytes; foreach (string file in fileNames) { ZipEntry entry = new ZipEntry(relativePath + PathGetFileName(file)); entryDateTime = DateTimeNow; sPutNextEntry(entry); using (FileStream fs = FileOpenRead(file)) { do { sourceBytes = fsRead(buffer bufferLength); sWrite(buffer sourceBytes); } while (sourceBytes > ); } } foreach (string dirName in dirNames) { string relativeDirPath = dirNameReplace(root ); ZipEntry entry = new ZipEntry(relativeDirPathReplace(\\ /) + /); sPutNextEntry(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 = extractPathTrimEnd(\\) + \\; byte[] data = new byte[bufferSize]; int size; using (ZipInputStream s = new ZipInputStream(FileOpenRead(zipFilePath))) { ZipEntry entry; while ((entry = sGetNextEntry()) != null) { string directoryName = PathGetDirectoryName(entryName); string fileName = PathGetFileName(entryName); //先创建目录 if (directoryNameLength > ) { DirectoryCreateDirectory(extractPath + directoryName); } if (fileName != StringEmpty) { using (FileStream streamWriter = FileCreate(extractPath + entryNameReplace(/ \\))) { while (true) { size = sRead(data dataLength); if (size > ) { streamWriterWrite(data size); } else { break; } } } } } } } } 网上找了很久 包括老外的site也找了 没找到现成的 自己写了一个 使用方法 使用方法 Zip z = new Zip(); zExtract(c:\\cczipc:\\haha ); zExtract(cczipc:\\haha ); zExtract(c:\\ccziphaha ); //上面的解压缩没啥大问题 zCompressDirectory(E:\\DotNet_Library\\SharpZipLib\\SharpZipLib__SourceSamples c:\\aazip ); 这个代码有一点点小问题 没有异常处理 压缩过程中 有文件被编辑或者被删除 那就无法压缩了 如果这样那也会异常 zCompressDirectory(C:\\CC C:\\CC\\zip ); |