ICSharpCode.SharpZipLib.dll 该压缩组件 可以将几个txt文件压缩为 一个zip 文件吗? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以的。。给你推荐一篇文章你看看http://blog.sina.com.cn/s/blog_59fa1c660100ctir.html?retcode=0 如果没有看懂,我原来做过把很多execl打包载的有源码 可以给你参考哈 msn:[email protected] using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.Checksums;namespace CSharpZipLibHelper{ public class SharpZipLibHelper { // <summary> /// 压缩指定文件生成ZIP文件 /// </summary> /// <param name="topDirName">顶层文件夹名称 \Storage Card\PDADataExchange\send\xml\</param> /// <param name="fileNamesToZip">待压缩文件列表 file名 ddd.txt</param> /// <param name="ZipedFileName">ZIP文件 \Storage Card\PDADataExchange\send\zip\version.zip</param> /// <param name="CompressionLevel">压缩比 7</param> /// <param name="password">密码 ""</param> /// <param name="comment">压缩文件注释文字 ""</param> public static void ZipFile(string topDirName, string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password, string comment) { using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(ZipedFileName, FileMode.Create))) { if (password != null && password.Length > 0) s.Password = password; if (comment != null && comment.Length > 0) s.SetComment(comment); s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression foreach (string file in fileNamesToZip) { using (FileStream fs = File.OpenRead(Path.Combine(topDirName, file))) //打开待压缩文件 { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); //读取文件流 ZipEntry entry = new ZipEntry(file); //新建实例 entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } s.Finish(); s.Close(); } } /// <summary> /// 解压缩ZIP文件到指定文件夹 /// </summary> /// <param name="zipfileName">ZIP文件</param> /// <param name="UnZipDir">解压文件夹</param> /// <param name="password">压缩文件密码</param> public static void UnZipFile(string zipfileName, string UnZipDir, string password) { //zipfileName=@"\Storage Card\PDADataExchange\receive\zip\test.zip"; //UnZipDir= @"\Storage Card\PDADataExchange\receive\xml\"; //password=""; ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName)); if (password != null && password.Length > 0) s.Password = password; try { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(UnZipDir); string pathname = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题 directoryName = directoryName + "\\" + pathname; Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(directoryName + "\\" + fileName); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); } catch (Exception eu) { throw eu; } finally { s.Close(); } } }}用ICSharpCode.SharpZipLib.dll加上上面的类调用 public string Compress(string RootPath, List<string> filename) { // 压缩 string Name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip"; string FileName = Path.Combine(RootPath, Name); string topPath = Path.GetPathRoot(RootPath); SharpZipLibHelper.ZipFile(RootPath, filename.ToArray(), FileName, 7, "", ""); return FileName; } 请教多系统共享用户解决方案,请有经验的高手指教! vs2008建立网站用vs调试的时候没有问题,发布到iis上之后,用iis浏览只能读数据库中的内容不能写入数据库 app_offline.htm是什么东西?为什么在我的web项目里我把它删掉了还是会再次生成? DetailsView 删除问题 事物 设计时有checkbox,运行时就没有了,我是拖放过去的,估计是html的问题,所以把html代码复制过来了 后台变量传给前台JavaScript的问题,请高手指点! 大家看看我的项目做的怎么样? detailsview小问题(很困扰) 菜妹的简单问题:) Eval的问题, 怪异的空异常问题
如果没有看懂,我原来做过把很多execl打包载的有源码 可以给你参考哈 msn:[email protected]
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;namespace CSharpZipLibHelper
{
public class SharpZipLibHelper
{
// <summary>
/// 压缩指定文件生成ZIP文件
/// </summary>
/// <param name="topDirName">顶层文件夹名称 \Storage Card\PDADataExchange\send\xml\</param>
/// <param name="fileNamesToZip">待压缩文件列表 file名 ddd.txt</param>
/// <param name="ZipedFileName">ZIP文件 \Storage Card\PDADataExchange\send\zip\version.zip</param>
/// <param name="CompressionLevel">压缩比 7</param>
/// <param name="password">密码 ""</param>
/// <param name="comment">压缩文件注释文字 ""</param>
public static void ZipFile(string topDirName, string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password, string comment)
{
using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(ZipedFileName, FileMode.Create)))
{
if (password != null && password.Length > 0)
s.Password = password; if (comment != null && comment.Length > 0)
s.SetComment(comment); s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression
foreach (string file in fileNamesToZip)
{
using (FileStream fs = File.OpenRead(Path.Combine(topDirName, file))) //打开待压缩文件
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); //读取文件流
ZipEntry entry = new ZipEntry(file); //新建实例
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
s.Finish();
s.Close();
}
} /// <summary>
/// 解压缩ZIP文件到指定文件夹
/// </summary>
/// <param name="zipfileName">ZIP文件</param>
/// <param name="UnZipDir">解压文件夹</param>
/// <param name="password">压缩文件密码</param>
public static void UnZipFile(string zipfileName, string UnZipDir, string password)
{
//zipfileName=@"\Storage Card\PDADataExchange\receive\zip\test.zip";
//UnZipDir= @"\Storage Card\PDADataExchange\receive\xml\";
//password=""; ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName));
if (password != null && password.Length > 0)
s.Password = password;
try
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(UnZipDir);
string pathname = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name); //生成解压目录
pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题
directoryName = directoryName + "\\" + pathname;
Directory.CreateDirectory(directoryName); if (fileName != String.Empty)
{
//解压文件到指定的目录
FileStream streamWriter = File.Create(directoryName + "\\" + fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
} else
{
break;
} }
streamWriter.Close();
}
}
s.Close();
}
catch (Exception eu)
{
throw eu;
} finally
{
s.Close();
}
}
}
}
用ICSharpCode.SharpZipLib.dll
加上上面的类
调用 public string Compress(string RootPath, List<string> filename)
{
// 压缩
string Name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip";
string FileName = Path.Combine(RootPath, Name);
string topPath = Path.GetPathRoot(RootPath); SharpZipLibHelper.ZipFile(RootPath, filename.ToArray(), FileName, 7, "", ""); return FileName;
}