zoukankan      html  css  js  c++  java
  • 关于ZipOupputStream添加压缩包常见问题

    其实园子压缩解压缩的方法很多,ZipOupputStream这个类的说明很多,我这边也是从网上找的代码,但是我在压缩的时候遇到了常见的两个问题,第一个就是压缩的时候读取压缩包报该压缩包已经在另一个进程打开的bug,这个问题解决方法是不能把创建的压缩包和被压缩的文件放在同一目录下,因为当前目录已经被打开了。

    第二个问题压缩文件夹无法把多个文件压缩,遍历无法压缩多个文件,原因是ZipOutputStream这个类库中的CRC32这个类,本人表示查来查去也没找个那个大神的园子里解释过这个类,总之把与这个类相关的代码注释掉就可以多个文件压缩了,希望有懂得大神可以给解释下。

    public class ZipFloClass
    {
    public void ZipFile(string strFile, string strZip)
    {
    if(File.Exists(strZip))
    {
    File.Delete(strZip);
    }
    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
    strFile += Path.DirectorySeparatorChar;
    FileStream fs = new FileStream(strZip, FileMode.Create);
    ZipOutputStream s = new ZipOutputStream(fs);
    s.SetLevel(6); // 0 - store only to 9 - means best compression
    zip(strFile, s, strFile);
    s.Finish();
    s.Close();

    }

    private void zip(string strFile, ZipOutputStream s, string staticFile)
    {
    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
    Crc32 crc = new Crc32();
    string[] filenames = Directory.GetFileSystemEntries(strFile);
    foreach (string file in filenames)
    {

    if (Directory.Exists(file))
    {
    zip(file, s, staticFile);
    }

    else // 否则直接压缩文件
    {
    //打开压缩文件
    FileStream fs = File.OpenRead(file);

    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    string tempfile = file.Substring(staticFile.LastIndexOf("\") + 1);
    ZipEntry entry = new ZipEntry(tempfile);

    entry.DateTime = DateTime.Now;
    entry.Size = fs.Length;
    fs.Close();
    //crc.Reset();
    //crc.Update(buffer);
    //entry.Crc = crc.Value;
    s.PutNextEntry(entry);
    s.Write(buffer, 0, buffer.Length);

    }
    }
    }

    }

  • 相关阅读:
    反射
    Ajax
    JSP(二)
    JSP
    Servlet(三)
    Servlet(二)
    Servlet
    idea的Tomcat的配置
    使用Idea创建Maven构造的Web工程
    Maven的下载、安装与环境配置
  • 原文地址:https://www.cnblogs.com/renzhitian/p/7903119.html
Copyright © 2011-2022 走看看