zoukankan      html  css  js  c++  java
  • 解压方法,用ZipInputStream的

    解压方法,用ZipInputStream的
    http://www.myexceptions.net/c-sharp/319376.html

    求一个解压方法,用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 (Exception ex)
    {
    err = ex.Message;
  • 相关阅读:
    nodeJS入门01-http模块
    nodeJS入门-Buffer对象
    php与MySQL(php内置mysql函数)
    php与MySQL(基本操作)
    log4net
    js验证小数类型(浮点数)和整数类型
    牛腩学ASP.NET CORE做博客视频
    opencv再学习之路(八)---设定感兴趣区域(RIO)
    opencv再学习之路(四)---色彩分割得到二值图像
    opencv再学习之路(三)---形态学操作
  • 原文地址:https://www.cnblogs.com/sunny3158/p/15235335.html
Copyright © 2011-2022 走看看