下面是压缩及解压缩的方法,不过解压缩我没有试验成功
回复人: iFei(一日朋友) ( ) 信誉:99  2003-6-1 12:49:33  得分:0  
 下载地址:
http://prdownloads.sourceforge.net/sharpdevelop/050SharpZipLib_SourceSamples.zip?download引用如下:
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;压缩解压方法:
public 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 void UnZipFile(string strFileName)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(strFileName));

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

string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName      = Path.GetFileName(theEntry.Name);

// create directory
// if (directoryName=="")
// directoryName=Application.StartupPath+"\\"+strFileName.Substring(0,strFileName.IndexOf(".",0));
// Directory.CreateDirectory(directoryName);

if (fileName != String.Empty) 
{
FileStream streamWriter = File.Create(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();
}

解决方案 »

  1.   

    I am not familiar with SharpZipLib, but here are some suggestions (just for demo, not tested, so it might not work):
    after you upload the file, HttpPostedFile MyFile = File1.PostedFile;MemoryStream ms = new MemoryStream();
    ZipOutputStream ZipStream = new ZipOutputStream(ms);int FileLen = MyFile.ContentLength;
    byte[] input = new byte[FileLen];Stream MyStream = MyFile.InputStream;MyStream.Read(input, 0, FileLen);
    ZipStream.Write(input,0,FileLen);ms.Position = 0;
    byte[] blist = ms.ToArray();//save the data to the database use the code here
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158when you are ready to unzip, use the method in the above article to retrieve the bytescreate a memorystreamMemoryStream ms = new MemoryStream(TheBytesFromTheDatabase);
    ms.Position = 0;
    ZipInputStream s = new ZipInputStream(ms);
    ...//unzip it
      

  2.   

    谢谢思归
    代码出错了:no open entry.
    后来我加上
    ZipEntry ZipEntry = new ZipEntry("ZippedFile");
    ZipStream.PutNextEntry(ZipEntry);
    ZipStream.SetLevel(1);
    不出错了,但压缩后不正确,文件长度由40K左右变为40byte(ms.length)
      

  3.   

    加上ZipEntry ZipEntry = new ZipEntry("ZippedFile");
    ZipStream.PutNextEntry(ZipEntry);
    ZipStream.SetLevel(1);
    好像也不行吧,你再試一下 ??
      

  4.   

    看看我发的贴子吧;或许对你有帮助
    http://expert.csdn.net/Expert/topic/2377/2377844.xml?temp=.6736261