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();
}
{
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();
}
解决方案 »
- Ext.net1.6弹出没有许可的框框怎么解决?
- 求sql语句
- 麻烦帮我看看这段代码有什么错,没分,请间量,相互学习
- 怎么设置在一个固定时间段时,半个整点调用某个方法?
- 怎么识别是一中文汉字还是两个字符呢?
- 各位中有没有既会用c#又会用delphi的,问个简单的问题!
- 如何将一个datatable中的数据不用循环语句直接全部存到数据库中?
- dotnetbar中ComboBoxEx控件item项的对齐方式
- 周五了无聊问个可能很幼稚的问题,大神不要笑
- 问一下“new”的问题
- 如何能够不用登陆到论坛而跟踪自己的帖子呢
- 怎样选折一个表的最后一条记录?例如第一条是select top 1 * from Customers
我是这样调用的:
string zipfile=@"e:\test.rar";
string destinationpath=@"e:\";
d.UnZip(zipfile,destinationpath);
但出现错误:
未处理的"ICSharpCode.SharpZipLib.ZipException"类型的异常出现在icsharpcode.sharpziplib.dll中
其它信息:Wrong Local header signature:0x21726152
while ((theEntry = s.GetNextEntry()) != null)
其中P为50,K为4B你得出来的头是0x21726152,显然不是ZIP文件,应该还是RAR,或者只是文件名后缀变成了ZIP
既是一种控制台的程序.
http://members.shaw.ca/jcarto/ZipSharp/index.htm希望对你有帮助:)