在.net中实现压缩多个文件为.zip文件

2014/3/8 已被4042人阅读

当前位置:首页>新闻中心>技术关注【关闭】

这段时间一直在做一个网站,其中遇到了一个问题,就是在服务器端压缩多个服务器端的文件,然后在供客户下载。说白了就是用户上传了多个文件,然后别的用户可以点击批量下载这些文件。我要做的就是实现把这些文件压缩之后供用户下载。
我首先想到的是.Net提供的GZipStream类,翻了一下书才发现GZipStream没有提供加压多个文件的方法,需要自己定义,这样解压也只有使用自己的程序才可以。这不是我想要的效果,放弃这个方案。由于之前没有接触过这方面的技术,只有在网上找,结果找到多种解决的方案,大部分的方法都要使用外部的类库。这里列出来一个最常用的,以备以后查找方便:
SharpZipLib
(参见文章
http://www.cnblogs.com/tuyile006/archive/2008/04/25/1170894.html

使用ICSharpCode.SharpZipLib.dll;
下载地址
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
下面是对#ZipLib进行.net下的解压缩的方法的介绍。
1.BZip2
加入ICSharpCode.SharpZipLib.dll的引用,在#Develop的安装目录下的\SharpDevelop\bin目录下。然后在程序中使用using语句把BZip2
类库包含进来。
压缩:使用BZip2的静态方法Compress。
它的第一个参数是所要压缩的文件所代表的输入流,可以使用System.IO.File的静态方法OpenRead。
第二个参数是要建立的压缩文件所代表的输出流,可以使用System.IO.File的静态方法Create创建,压缩文件名是所要压缩文件的文件名
加上压缩后缀.bz(同样你也可以取其他的文件名)。
第三个参数是要压缩的块大小(一般为2048的整数)。
解压:使用BZip2的静态方法Decompress。
它的第一个参数是所要解压的压缩文件所代表的输入流,可以使用System.IO.File的静态方法OpenRead。
第二个参数是要建立的解压文件所代表的输出流,可以使用System.IO.File的静态方法Create创建,因为解压文件的文件名是去掉了压缩
文件扩展名的压缩文件名(你也可以做成解压文件与压缩文件不同名的)。
编译你的程序,然后在命令行方式下输入bzip2 文件名(假设建立的C#文件是bzip2,就可以生成压缩文件;输入bzip2 -d 文件名,就会解压
出文件来(-d是用来表示解压,你也可以使用其他的符号)。
呵呵,原来做压缩可以这么简单的,压缩效果也可以啊。
using System;
using System.IO;
using ICSharpCode.SharpZipLib.BZip2;
class MainClass
{
public static void Main(string[] args)
{
if (args[0] == "-d") { // 解压
BZip2.Decompress(File.OpenRead(args[1]), File.Create(Path.GetFileNameWithoutExtension(args[1])));
} else { //压缩
BZip2.Compress(File.OpenRead(args[0]), File.Create(args[0] + ".bz"), 4096);
}
}
}
2.GZip
加入ICSharpCode.SharpZipLib.dll的引用,在#Develop的安装目录下的\SharpDevelop\bin目录下。然后在程序中使用using语句把GZip类
库包含进来。
由于GZip没有BZip2的简单解压缩方法,因此只能使用流方法来进行解压缩。具体的方法见程序的说明。
编译程序,然后在命令行方式下输入GZip 文件名(假设建立的C#文件是GZip,就可以生成压缩文件;输入GZip -d 文件名,就会解压出文
件来(-d是用来表示解压,你也可以使用其他的符号)。
using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;
class MainClass
{
public static void Main(string[] args)
{
if (args[0] == "-d") { // 解压
Stream s = new GZipInputStream(File.OpenRead(args[1]));
//生成一个GZipInputStream流,用来打开压缩文件。
//因为GZipInputStream由Stream派生,所以它可以赋给Stream。
//它的构造函数的参数是一个表示要解压的压缩文件所代表的文件流
FileStream fs = File.Create(Path.GetFileNameWithoutExtension(args[1]));
//生成一个文件流,它用来生成解压文件
//可以使用System.IO.File的静态函数Create来生成文件流
int size = 2048;//指定压缩块的大小,一般为2048的倍数
byte[] writeData = new byte[size];//指定缓冲区的大小
while (true) {
size = s.Read(writeData, 0, size);//读入一个压缩块
if (size > 0) {
fs.Write(writeData, 0, size);//写入解压文件代表的文件流
} else {
break;//若读到压缩文件尾,则结束
}
}
s.Close();
} else { // 压缩
Stream s = new GZipOutputStream(File.Create(args[0] + ".gz"));
//生成一个GZipOutputStream流,用来生成压缩文件。
//因为GZipOutputStream由Stream派生,所以它可以赋给Stream。
FileStream fs = File.OpenRead(args[0]);
/生成一个文件流,它用来打开要压缩的文件
//可以使用System.IO.File的静态函数OpenRead来生成文件流
byte[] writeData = new byte[fs.Length];
//指定缓冲区的大小
fs.Read(writeData, 0, (int)fs.Length);
//读入文件
s.Write(writeData, 0, writeData.Length);
//写入压缩文件
s.Close();
//关闭文件
}
}
}
使用这个类库固然好,但是也有些缺陷,它只能压缩文件夹第一级子目录中的“文件”(不包括文件夹和子目录)的情况,这也不能满足我的要求,我想要的是可以压缩任意路径的多个文件。
没办法,只好再想别的办法。郁闷了很久之后在别人的一篇文章中终于找到了灵感,别人的一篇文章是写在java中实现压缩zip文件,我看了后发现在java中实现压缩为zip文件很容易。灵机一动我想到了.net中的J#,J#中应该有java中的这样类,如果有的话,那么我在我的C#程序中就可以引用了(利用.net特有的语言互操作性)。于是我就上网搜这方面的内容,终于在MSDN中找到了这样的例子(http://msdn.microsoft.com/en-gb/library/aa686114(zh-cn).aspx#EHAA得来全不费功夫啊),贴出来找到的代码,大家共同学习。

using System;
using System.Collections;
using java.util;
using java.util.zip;

namespace CsZip
{
public delegate Enumeration EnumerationMethod();

///


/// Wraps java enumerators
///

public class EnumerationAdapter : IEnumerable
{
private class EnumerationWrapper : IEnumerator
{
private EnumerationMethod m_Method;
private Enumeration m_Wrapped;
private object m_Current;

public EnumerationWrapper(EnumerationMethod method)
{
m_Method = method;
}

// IEnumerator
public object Current
{
get { return m_Current; }
}

public void Reset()
{
m_Wrapped = m_Method();
if (m_Wrapped == null)
throw new InvalidOperationException();
}

public bool MoveNext()
{
if (m_Wrapped == null)
Reset();
bool Result = m_Wrapped.hasMoreElements();
if (Result)
m_Current = m_Wrapped.nextElement();
return Result;
}
}

private EnumerationMethod m_Method;

public EnumerationAdapter(EnumerationMethod method)
{
if (method == null)
throw new ArgumentException();
m_Method = method;
}

// IEnumerable
public IEnumerator GetEnumerator()
{
return new EnumerationWrapper(m_Method);
}
}

public delegate bool FilterEntryMethod(ZipEntry e);

///


/// Zip stream utils
///

public class ZipUtility
{
public static void CopyStream(java.io.InputStream from, java.io.OutputStream to)
{
sbyte[] buffer = new sbyte[8192];
int got;
while ((got = from.read(buffer, 0, buffer.Length)) > 0)
to.write(buffer, 0, got);
}

public static void ExtractZipFile(ZipFile file, string path, FilterEntryMethod filter)
{
foreach (ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(file.entries)))
{
if (!entry.isDirectory())
{
if ((filter == null || filter(entry)))
{
java.io.InputStream s = file.getInputStream(entry);
try
{
string fname = System.IO.Path.GetFileName(entry.getName());
string newpath = System.IO.Path.Combine(path, System.IO.Path.GetDirectoryName(entry.getName()));

System.IO.Directory.CreateDirectory(newpath);

java.io.FileOutputStream dest = new java.io.FileOutputStream(System.IO.Path.Combine(newpath, fname));
try
{
CopyStream(s, dest);
}
finally
{
dest.close();
}
}
finally
{
s.close();
}
}
}
}
}

///


/// 创建新的Zip文件
///

/// Zip文件的路径
/// Zip文件的路径
public static ZipFile CreateEmptyZipFile(string fileName)
{
new ZipOutputStream(new java.io.FileOutputStream(fileName)).close();
return new ZipFile(fileName);
}

///


/// 向存在的Zip文件中添加待压缩的文件
///

/// Zip文件
///
/// 待压缩的文件的路径
///
public static ZipFile UpdateZipFile(ZipFile file, FilterEntryMethod filter, string[] newFiles)
{
string prev = file.getName();
string tmp = System.IO.Path.GetTempFileName();
ZipOutputStream to = new ZipOutputStream(new java.io.FileOutputStream(tmp));
try
{
CopyEntries(file, to, filter);
// add entries here
if (newFiles != null)
{
foreach (string f in newFiles)
{
ZipEntry z = new ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length));
z.setMethod(ZipEntry.DEFLATED);
to.putNextEntry(z);
try
{
java.io.FileInputStream s = new java.io.FileInputStream(f);
try
{
CopyStream(s, to);
}
finally
{
s.close();
}
}
finally
{
to.closeEntry();
}
}
}
}
finally
{
to.close();
}
file.close();

// now replace the old file with the new one
System.IO.File.Copy(tmp, prev, true);
System.IO.File.Delete(tmp);

return new ZipFile(prev);
}

public static void CopyEntries(ZipFile from, ZipOutputStream to)
{
CopyEntries(from, to, null);
}

public static void CopyEntries(ZipFile from, ZipOutputStream to, FilterEntryMethod filter)
{
foreach (ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(from.entries)))
{
if (filter == null || filter(entry))
{
java.io.InputStream s = from.getInputStream(entry);
try
{
to.putNextEntry(entry);
try
{
CopyStream(s, to);
}
finally
{
to.closeEntry();
}
}
finally
{
s.close();
}
}
}
}
}
}

预约洽谈

多一份方案,多一个参考,预约我们的商务面对面沟通,总有益处 周一至周五,9:00-18:00    咨询热线:021-3100 6558 24小时热线:15900965330

我要预约
电话咨询 在线沟通 QQ咨询 微信聊天