日期:2014-05-18  浏览次数:21261 次

求一个解压方法,用ZipInputStream的
下面是压缩方法,求一个对压缩后的byte[]的解压方法

public byte[] ZipPackFiles(string filenames)
  {
  ZipOutputStream zos = null;
  MemoryStream ms = null;
  try
  {
  ms = new MemoryStream();
  zos = new ZipOutputStream(ms);
  zos.SetLevel(9);
  FileStream fs = File.OpenRead(filenames);
  byte[] buffer = new byte[fs.Length];
  fs.Read(buffer, 0, buffer.Length);
  ZipEntry entry = new ZipEntry(filenames);
  entry.DateTime = DateTime.Now;
  entry.Size = fs.Length;
  fs.Close();
  zos.PutNextEntry(entry);
  zos.Write(buffer, 0, buffer.Length);
  zos.Finish();
  zos.Close();
  return ms.ToArray();
  }
  catch (Exception)
  {
  return null;
  }
  }

------解决方案--------------------
public bool UnZipFile(string zipFilePath, string unZipDir, out string err)
{
err = "";
if (zipFilePath == string.Empty)
{
err = "压缩文件不能为空!";
return false;
}
if (!File.Exists(zipFilePath))
{
err = "压缩文件不存在!";
return false;
}
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);

try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("\\"))
directoryName += "\\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + 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;
}
}
}
}
}
}
}
catch