急!急!c# 压缩多个文件成.zip后再解压存放到系统的某个目录

cqf8888 2009-09-16 03:27:58
急!急!c# 压缩多个文件成.zip后再解压存放到系统的某个目录
...全文
1548 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
leon28 2009-09-17
  • 打赏
  • 举报
回复
问题出在“ICSharpCode.SharpZipLib.DLL”上,这个DLL版本和你的冲突,去网上找个匹配的
cqf8888 2009-09-17
  • 打赏
  • 举报
回复
在程序引用里面加入了ICSharpCode.SharpZipLib.dll,还是出现这个错误。



WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

////////////
WebForm1.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Zip zip = new Zip();
zip.unZipFile(@"E:\33\11.zip", @"E:\44");

}
}
}




//////////zip.cs



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

public class Zip
{
public string unZipFile(string TargetFile, string fileDir)
{
string rootFile = " ";
try
{
//读取压缩文件(zip文件),准备解压缩
ZipInputStream s =new ZipInputStream(File.OpenRead(TargetFile.Trim()));
ZipEntry theEntry;
string path =fileDir; //解压出来的文件保存的路径

string rootDir = " "; //根目录下的第一个子文件夹的名称
while ((theEntry = s.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(theEntry.Name); //得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf( "\\")>=0)
{
rootDir = rootDir.Substring(0, rootDir.IndexOf( "\\ ") + 1);
}
string dir = Path.GetDirectoryName(theEntry.Name); //根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name); //根目录下的文件名称
if (dir != " " && fileName == " ") //创建根目录下的子文件夹,不限制级别
{
if (!Directory.Exists(fileDir + "\\ " + dir))
{
path = fileDir + "\\ " + dir; //在指定的路径创建文件夹
Directory.CreateDirectory(path);
}
}
else if (dir == " " && fileName != " ") //根目录下的文件
{
path = fileDir;
rootFile = fileName;
}
else if (dir != " " && fileName != " ") //根目录下的第一级子文件夹下的文件
{
if (dir.IndexOf( "\\ ") > 0) //指定文件保存的路径
{
path = fileDir + "\\ " + dir;
}
}

if (dir == rootDir) //判断是不是需要保存在根目录下的文件
{
path = fileDir + "\\ " + rootDir;
}

//以下为解压缩zip文件的基本步骤
//基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + "\\ " + 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();

return rootFile;
}
catch (Exception ex)
{
return "1; " + ex.Message;
}
}

}
马老虎 2009-09-17
  • 打赏
  • 举报
回复
我前俩天刚做了个 demo!
+QQ 124339316 给你发个
leon28 2009-09-17
  • 打赏
  • 举报
回复
行 1: <%@ OutputCache Duration="3600" VaryByParam="none"%>
行 2: <%@ Page language="c#" Codebehind="tree.aspx.cs" AutoEventWireup="false" Inherits="treeyingyong.tree" %>
行 3: <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>行 4: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
行 5: <HTML>

解决办法
行 3:改为即可搞掂
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
-------------------------
参考上面这个是不是你某个引用页面注册控件的时候写入版本信息?
cqf8888 2009-09-17
  • 打赏
  • 举报
回复
10楼的朋友,你这两个函数的参数分别什么意思,能否写具体点,还有是否可以压缩成.zip文件。谢谢!

CompressRAR(string patch, string rarPatch, string rarName)
unCompressRAR(string unRarPatch, string rarPatch, string rarName)
cqf8888 2009-09-17
  • 打赏
  • 举报
回复
1楼的朋友。你那个控制台程序提示报错,数组超出范围。 if (args[0] == "-d")此处报错。
winner2050 2009-09-17
  • 打赏
  • 举报
回复
    public class Winrar
{
/// <summary>
/// 是否安装了Winrar
/// </summary>
/// <returns></returns>
static public bool Exists()
{
RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
}
/// <summary>
/// 打包成Rar
/// </summary>
/// <param name="patch"></param>
/// <param name="rarPatch"></param>
/// <param name="rarName"></param>
public string CompressRAR(string patch, string rarPatch, string rarName)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
Directory.CreateDirectory(patch);
//命令参数
//the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
the_Info = " a " + rarName + " " + patch + " -r"; ;
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录
the_StartInfo.WorkingDirectory = rarPatch;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return string.Empty;
}
/// <summary>
/// 解压
/// </summary>
/// <param name="unRarPatch"></param>
/// <param name="rarPatch"></param>
/// <param name="rarName"></param>
/// <returns></returns>
public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;


try
{
the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
//the_rar = the_rar.Substring(1, the_rar.Length - 7);

if (Directory.Exists(unRarPatch) == false)
{
Directory.CreateDirectory(unRarPatch);
}
the_Info = "x \"" + rarName + "\" \"" + unRarPatch + "\" -y";

ProcessStartInfo the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径

Process the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return string.Empty;
}
}
cqf8888 2009-09-17
  • 打赏
  • 举报
回复
谢谢楼上的朋友。不过才20分,你们每人10分。真不好意思。
cqf8888 2009-09-16
  • 打赏
  • 举报
回复
不管是在控制台程序还是在web程序都出现如上的错误。
cqf8888 2009-09-16
  • 打赏
  • 举报
回复
补充下上面的:就是我在web文件如何解压与压缩。谢谢!
cqf8888 2009-09-16
  • 打赏
  • 举报
回复
非常感谢楼上的朋友回复,不过我遇到了一个错误,我在asp.net添加ICSharpCode.SharpZipLib.dll后,编译没有报错,但是运行测试页面后出现错误:未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.85.4.369, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)。真不知道是什么原因,网上也没有查到。非常感谢!能否请教此错误原因。
leon28 2009-09-16
  • 打赏
  • 举报
回复
使用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();
//关闭文件
}
}
}

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧