public static void UnzipBytes(byte[] zipedBytes, string fileName)
{
ZipInputStream s = new ZipInputStream(new System.IO.MemoryStream(zipedBytes));

System.IO.FileStream streamWriter = File.Create(fileName);

s.GetNextEntry();
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();
} public static byte[] ZipBytes(byte[] byteToZip)
{
string tempFile = System.IO.Path.GetTempFileName();
System.IO.FileStream fs = new System.IO.FileStream(tempFile, System.IO.FileMode.Open);
ZipOutputStream zipOut = new ZipOutputStream(fs); ZipEntry entry = new ZipEntry("LogStream");
zipOut.PutNextEntry(entry);
zipOut.Write(byteToZip, 0, byteToZip.Length); zipOut.Finish();
zipOut.Close();

fs = new System.IO.FileStream(tempFile, System.IO.FileMode.Open);
byte[] zipedBytes = new Byte[fs.Length];
fs.Read(zipedBytes, 0, (int)fs.Length);
fs.Close(); return zipedBytes;
} private static 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();
} private static ZipOutputStream zos; /// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="directoryName">文件夹名称</param>
public static void ZipDirectory(string directoryName, string zipFileName)
{
System.IO.FileStream ZipFile=System.IO.File.Create(zipFileName);
zos = new ZipOutputStream(ZipFile);
addZipEntry(directoryName, String.Empty);
zos.Finish();
zos.Close();
} private static void addZipEntry(string PathStr, string zipPath)
{
System.IO.DirectoryInfo di= new System.IO.DirectoryInfo(PathStr);

string dirEntryName = zipPath + di.Name + "/";
ZipEntry dirEntry = new ZipEntry(dirEntryName);
zos.PutNextEntry(dirEntry); foreach(System.IO.DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName, zipPath + di.Name + "\\");
}

foreach(System.IO.FileInfo item in di.GetFiles())
{
System.IO.FileStream fs = System.IO.File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName=dirEntryName + item.Name; ZipEntry entry = new ZipEntry(strEntryName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
} /// <summary>
/// 生成压缩文件
/// </summary>
/// <param name="fileNameList">待压缩的文件名列表</param>
/// <param name="zipFileName">生成的压缩的文件</param>
public static void ZipFiles(string fileNameList, string zipFileName)
{
System.IO.FileStream ZipFile=System.IO.File.Create(zipFileName);
ZipOutputStream zipOut = new ZipOutputStream(ZipFile); string[] fileNames = fileNameList.Split('|');
foreach(string fileName in fileNames)
{
System.IO.FileStream fs = System.IO.File.OpenRead(fileName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
ZipEntry entry = new ZipEntry(fi.Name);
zipOut.PutNextEntry(entry);
zipOut.Write(buffer, 0, buffer.Length);
fs.Close();
} zipOut.Finish();
zipOut.Close();
ZipFile.Close();
}// public static void UnZipFiles(string zipFileName, string extractPath)
// {
// } public static void UnZip(string zipFile, string destinationDirectory)
{
ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(zipFile));
//System.IO.FileInfo fi = new System.IO.FileInfo (zipFile);

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) 
{
string directoryName = destinationDirectory + "\\" + System.IO.Path.GetDirectoryName(theEntry.Name);
string fileName      = System.IO.Path.GetFileName(theEntry.Name);

// create directory
System.IO.Directory.CreateDirectory(directoryName);

if (fileName != String.Empty) 
{
System.IO.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();
}

解决方案 »

  1.   

    idiotzeng(白痴) ( ) :
    我是这样调用的:
    string zipfile=@"e:\test.rar";
    string destinationpath=@"e:\";
    d.UnZip(zipfile,destinationpath);
    但出现错误:
    未处理的"ICSharpCode.SharpZipLib.ZipException"类型的异常出现在icsharpcode.sharpziplib.dll中
    其它信息:Wrong Local header signature:0x21726152
      

  2.   

    错误指在
    while ((theEntry = s.GetNextEntry()) != null)
      

  3.   

    换成zip格式也不行。还是那错误
      

  4.   

    ZIP文件的头是0x4034b50,即 'P' | ('K' << 8) | (3 << 16) | (4 << 24)
    其中P为50,K为4B你得出来的头是0x21726152,显然不是ZIP文件,应该还是RAR,或者只是文件名后缀变成了ZIP
      

  5.   

    可不可以写成一种脱离SharpZipLib.dll而可以运行的程序.
    既是一种控制台的程序.
      

  6.   

    Zip Sharp! Version 1.71 Beta
    http://members.shaw.ca/jcarto/ZipSharp/index.htm希望对你有帮助:)
      

  7.   

    >>脱离SharpZipLib.dll把SharpZipLib工程里的文件放到你的项目里变成一个就可以了
      

  8.   

    SharpZipLib是源码公开的,你可以把它的源码包括在你的项目里面编译!