SharpZipLib压缩文件夹及文件

对于之前发布的有关Net中使用压缩解压缩技术中,有个相当大的缺陷,是压缩文件时,不能递归压缩子文件夹。现将递归压缩的方法提供一下,还望大家多多探讨。(该代码参考别人写的例子修改了一下) 注意:请引用以下类库 using System; using System.Collections.Generic; using System.Text; using System.IO; using System.DirectoryServices; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.Checksums; public class ZipHelper { ......... #region 压缩文件夹,支持递归 /// /// 压缩文件夹 /// /// 待压缩的文件夹 /// 压缩后文件路径(包括文件名) /// 是否递归压缩 /// public static bool Compress(string dir, string targetFileName,bool recursive) { //如果已经存在目标文件,询问用户是否覆盖 if (File.Exists(targetFileName)) { if (!_ProcessOverwrite(targetFileName)) return false; } if (recursive == false) return Compress(dir, targetFileName); FileStream ZipFile; ZipOutputStream ZipStream; //open ZipFile = File.Create(targetFileName); ZipStream = new ZipOutputStream(ZipFile); if (dir != String.Empty) { _CompressFolder(dir, ZipStream, dir); } //close ZipStream.Finish(); ZipStream.Close(); if (File.Exists(targetFileName)) return true; else return false; } /// /// 压缩某个子文件夹 /// /// /// /// private static void _CompressFolder(string basePath, ZipOutputStream zips, string zipfolername) { if (File.Exists(basePath)) { _AddFile(basePath, zips, zipfolername); return; } string[] names = Directory.GetFiles(basePath); foreach (string fileName in names) { _AddFile(fileName, zips, zipfolername); } names = Directory.GetDirectories(basePath); foreach (string folderName in names) { _CompressFolder(folderName, zips, zipfolername); } } /// /// 压缩某个子文件 /// /// /// /// private static void _AddFile(string fileName, ZipOutputStream zips, string zipfolername) { if (File.Exists(fileName)) { _CreateZipFile(fileName,zips,zipfolername); } } /// /// 压缩单独文件 /// /// /// /// private static void _CreateZipFile(string FileToZip, ZipOutputStream zips,string zipfolername) { try { FileStream StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read); string temp = FileToZip; string temp1 = zipfolername; if (temp1.Length > 0) { int i = temp1.LastIndexOf('//') + 1; int j = temp.Length - i; temp = temp.Substring(i, j); } ZipEntry ZipEn = new ZipEntry(temp); zips.PutNextEntry(ZipEn); byte[] buffer = new byte[16384]; System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); zips.Write(buffer, 0, size); try { while (size < StreamToZip.Length) { int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); zips.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (System.Exception ex) { throw ex; } StreamToZip.Close(); } catch (Exception e) { throw e; } } #endregion ......... } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ntshenwh/archive/2007/12/20/1955084.aspx
node-stream-zip 是查看和提取大型 ZIP 文件的 Node.js 库。特性:从不加载完整的归档到内存,一切都是通过块读取大型归档支持所有操作都是非阻塞,非同步 i/o快速初始化无依赖,无二进制组件内置 zlib 模块解压deflate, deflate64, sfx, macosx/windows 内置归档ZIP64 支持安装$ npm install node-stream-zip使用var StreamZip = require('node-stream-zip');   var zip = new StreamZip({       file: 'archive.zip',       storeEntries: true     }); zip.on('error', function(err) { /*handle*/ }); zip.on('ready', function() {     console.log('Entries read: '   zip.entriesCount);     // stream to stdout     zip.stream('node/benchmark/net/tcp-raw-c2s.js', function(err, stm) {         stm.pipe(process.stdout);     });     // extract file     zip.extract('node/benchmark/net/tcp-raw-c2s.js', './temp/', function(err) {         console.log('Entry extracted');     });     // extract folder     zip.extract('node/benchmark/', './temp/', function(err, count) {         console.log('Extracted '   count   ' entries');     });     // extract all     zip.extract(null, './temp/', function(err, count) {         console.log('Extracted '   count   ' entries');     });     // read file as buffer in sync way     var data = zip.entryDataSync('README.md'); }); zip.on('extract', function(entry, file) {     console.log('Extracted '   entry.name   ' to '   file); }); zip.on('entry', function(entry) {     // called on load, when entry description has been read     // you can already stream this entry, without waiting until all entry descriptions are read (suitable for very large archives)      console.log('Read entry ', entry.name); });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值