如何用C#解压一个普通zip文件里的某一个文件!!!

567567 2006-07-09 01:20:02
搜索了好多!!还是不得其解!!!

怎样解压缩一个ZIP里的某一个文件!!(顺便问一下RAR压缩解压)

请不要告诉我调用其他解压软件!!如 winrar 之类!!!谢谢!!!!

Gzip好像不能解压普通的zip文件????





...全文
1506 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yurenjf 2007-02-06
  • 打赏
  • 举报
回复
mark
yurenjf 2006-08-03
  • 打赏
  • 举报
回复
learning...
567567 2006-07-10
  • 打赏
  • 举报
回复
/**//// <summary>
/// 压缩文件
/// </summary>

using System;
using System.IO;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace Compression
{
public class ZipClass
{

public void ZipFile(string FileToZip, string ZipedFile ,int CompressionLevel, int BlockSize)
{
//如果文件没有找到,则报错
if (! System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}

System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size =StreamToZip.Read(buffer,0,buffer.Length);
ZipStream.Write(buffer,0,size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead =StreamToZip.Read(buffer,0,buffer.Length);
ZipStream.Write(buffer,0,sizeRead);
size += sizeRead;
}
}
catch(System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}

public void ZipFileMain(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]);

Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));

s.SetLevel(6); // 0 - store only to 9 - means best compression

foreach (string file in filenames)
{
//打开压缩文件
FileStream fs = File.OpenRead(file);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);

entry.DateTime = DateTime.Now;

// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fs.Length;
fs.Close();

crc.Reset();
crc.Update(buffer);

entry.Crc = crc.Value;

s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);

}

s.Finish();
s.Close();
}
}
}

现在再来看看解压文件类的源码

/**//// <summary>
/// 解压文件
/// </summary>

using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;

using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;

namespace DeCompression
{
public class UnZipClass
{
public void UnZip(string[] args)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{

string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name);

//生成解压目录
Directory.CreateDirectory(directoryName);

if (fileName != String.Empty)
{
//解压文件到指定的目录
FileStream streamWriter = File.Create(args[1]+theEntry.Name);

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();
}
}
}

有了压缩和解压缩的类以后,就要在窗体里调用了。怎么?是新手,不会调用?Ok,接着往下看如何在窗体里调用。

首先在窗体里放置两个命令按钮(不要告诉我你不会放啊~),然后编写以下源码

/**//// <summary>
/// 调用源码
/// </summary>

private void button2_Click_1(object sender, System.EventArgs e)
{
string []FileProperties=new string[2];
FileProperties[0]="C:\unzipped\";//待压缩文件目录
FileProperties[1]="C:\zip\a.zip"; //压缩后的目标文件
ZipClass Zc=new ZipClass();
Zc.ZipFileMain(FileProperties);
}

private void button2_Click(object sender, System.EventArgs e)
{
string []FileProperties=new string[2];
FileProperties[0]="C:\zip\test.zip";//待解压的文件
FileProperties[1]="C:\unzipped\";//解压后放置的目标目录
UnZipClass UnZc=new UnZipClass();
UnZc.UnZip(FileProperties);
}

在网上找的!!!

修改一下
string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name);

//生成解压目录
Directory.CreateDirectory(directoryName);

if (fileName != String.Empty)
{
//解压文件到指定的目录
FileStream streamWriter = File.Create(args[1]+theEntry.Name);
这部分就可以了!!!!

谢谢各位!!!!!!!






567567 2006-07-10
  • 打赏
  • 举报
回复
谢谢!!我还在找怎样将某一个文件解压出来,还有怎么有几层文件夹的解压会出错呢?

麻烦谁能贴段代码!!! SharpZipLib 刚开始学习!!!谢谢!!!!
Yuna_2z 2006-07-10
  • 打赏
  • 举报
回复
UP
takemoto 2006-07-10
  • 打赏
  • 举报
回复
ICSharpCode.SharpZipLib不错,但是有一个问题:在解压缩WinRAR生成并加密码后的ZIP文件时会出错,不知道有什么解决办法.
marvelstack 2006-07-10
  • 打赏
  • 举报
回复
通过 C# 使用 J# 类库中的 Zip 类压缩文件和数据
http://www.microsoft.com/china/msdn/library/langtool/vcsharp/miszipcompression.mspx


使用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();
//关闭文件
}
}
}
Compress Zip files with Windows Shell API and C#
http://www.codeproject.com/csharp/CompressWithWinShellAPICS.asp
foyuan 2006-07-09
  • 打赏
  • 举报
回复
SharpZipLib
很出名的组件
你下载下来看看
singlepine 2006-07-09
  • 打赏
  • 举报
回复
http://singlepine.cnblogs.com/archive/2006/01/06/312475.html
fangzhe 2006-07-09
  • 打赏
  • 举报
回复
你看看SharpZipLib,能解/压不少格式
gz和winzip算法是相似的,但还是不同的

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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