【问题标题】:De-compressing a ZIP file using Java ZipFile class使用 Java ZipFile 类解压缩 ZIP 文件
【发布时间】:2012-03-22 20:08:48
【问题描述】:

我在使用 Java 解压缩 ZIP 文件时遇到问题。方法如下。

文件解压缩后文件结构正确,这意味着 ZIP 文件中的目录正常,但文件输出的长度为零。

我检查了 ZIP 文件,看看压缩是否正确,那里都正确。

如果有人看到我错过的东西,请...

public static void unzip ( File zipfile, File directory ) throws IOException {
  ZipFile zip = new ZipFile ( zipfile );
  Enumeration<? extends ZipEntry> entries = zip.entries ();

  while ( entries.hasMoreElements () ) {
    ZipEntry entry = entries.nextElement ();
    File file = new File ( directory, entry.getName () );

    if ( entry.isDirectory () ) {
      file.mkdirs ();
    }
    else {
      file.getParentFile ().mkdirs ();

      ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );
      OutputStream out = new FileOutputStream ( file );
      byte[] buffer = new byte[4096];
      int readed = 0;

      while ( ( readed = in.read ( buffer ) ) > 0 ) {
        out.write ( buffer, 0, readed );
        out.flush ();
      }

      out.close ();
      in.close ();
    }
  }

  zip.close ();
}

更多...显然方法 getInputStream ( entry ) 正在返回零字节,不知道究竟是为什么。

【问题讨论】:

  • 你试过调试吗? in.read() 调用是否返回一些字节?
  • 不要在循环内刷新。

标签: java zip compression


【解决方案1】:

ZipFile已经解压了一个条目的数据,也不需要使用ZipInputStream

代替:

ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );

用途:

InputStream in = zip.getInputStream ( entry );

ZipInputStream 也可用于解压缩 ZIP 文件。您获得零长度流的原因是因为使用 ZipInputStream 您需要调用 getNextEntry() 来读取 ZIP 中的第一个文件。

【讨论】:

  • 谢谢,确实是这个问题...尝试解压缩已经解压缩的流。
【解决方案2】:

以下代码有效:

package so;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class TestZip {

    public static void main(String[] args) {
        String path = "C:" + File.separator + "tmp" + File.separator;
        String nom = "demo.zip";
        File zipfile = new File(path + nom);
        File directory = new File(path);
        TestZip m = new TestZip();
        try {
            m.unzip(zipfile, directory);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public static void unzip ( File zipfile, File directory ) throws IOException {
        System.out.println(zipfile.toString());
        System.out.println(directory.toString());
          ZipFile zip = new ZipFile (  zipfile );
          System.out.println("1");
          Enumeration<? extends ZipEntry> entries = zip.entries ();
          System.out.println("2");

          while ( entries.hasMoreElements () ) {
              System.out.println("3");
            ZipEntry entry = entries.nextElement ();
            File file = new File ( directory, entry.getName () );

            if ( entry.isDirectory () ) {
              file.mkdirs ();
            }
            else {
              file.getParentFile ().mkdirs ();

              ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );
              OutputStream out = new FileOutputStream ( file );
              byte[] buffer = new byte[4096];
              int readed = 0;

              while ( ( readed = in.read ( buffer ) ) > 0 ) {
                out.write ( buffer, 0, readed );
                out.flush ();
              }

              out.close ();
              in.close ();
            }
          }

          zip.close ();
        }


}

所以我认为问题出在您传递的参数上。使用“new File(complete_path + filename)”创建参数“zipfile”。如果你只是用文件名创建它是行不通的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多