下面是压缩及解压缩的方法,不过解压缩我没有试验成功
回复人: 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();
}
回复人: 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();
}
解决方案 »
- 前台怎样用js 接收后台的ArrayList数组并循环遍历出来?
- 界面原型设计
- asp.net 高手来看看
- JS非数字,邮箱验证错误
- 如何实现在几个公司之间跳转!!!
- 简单的sql语句
- 【急:寻求帮助】更新页面加载数据问题!
- DataGrid的小问题。新手问题大家快来帮忙啊!
- 用什么方法可以实现这样的效果?
- 在ASP或ASP.NET中不能调用使用了内存映像技术的COM组件吗?
- “/school”应用程序中的服务器错误,请大家关注一下?
- 显示图片时 不能够获取正确 的 路径if((System.IO.Path.GetExtension(entry.Name)=="gif") || (System.IO.Path.GetExtension(entry.Name)
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
代码出错了:no open entry.
后来我加上
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(1);
不出错了,但压缩后不正确,文件长度由40K左右变为40byte(ms.length)
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(1);
好像也不行吧,你再試一下 ??
http://expert.csdn.net/Expert/topic/2377/2377844.xml?temp=.6736261