代码之家  ›  专栏  ›  技术社区  ›  samblake

读取jar文件中的zip文件

  •  7
  • samblake  · 技术社区  · 16 年前

    URL url = getClass().getResource(zipfile);
    ZipFile zip = new ZipFile(url.getFile().replaceAll("%20", " "));     
    Entry entry = zip.getEntry("file.txt");
    
    InputStream is = zip.getInputStream(entry);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    
    String line = reader.readLine();
    while (line != null) {
        // do stuff
    }
    

    ZipFile 现在失败了。我可以得到一个 InputStream 对于zip:但是我没有办法为条目本身获取输入流。

    InputStream is = getClass().getResourceAsStream(zipfile);
    ZipInputStream zis = new ZipInputStream(is);
    
    ZipEntry entry = zis.getNextEntry();
    while (entry != null && !entry.getName().equals("file.txt")) {
        entry = zis.getNextEntry();
    }
    

    但是我没有办法为条目本身获取输入流。我试图找到条目的长度并从 ZipInputStream

    有办法解决这个问题吗?还是我必须将zip文件移回核心项目?

    4 回复  |  直到 13 年前
        1
  •  4
  •   helios    16 年前

    条目可以为您提供内部zip文件的inputstream。

    InputStream innerzipstream = zip.getInputStream(entry);
    

    所以你可以用

    new ZipInputStream(innerzipstream);
    

    并要求ZipInputStream检索内部zip文件的内容(以有序方式,因为它是ZipInputStream,所以您没有随机访问权限)

    看看 http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipInputStream.html

    当ZipInputStream从输入流读取zip文件时,它必须按顺序进行操作:

    // DO THIS for each entry
    ZipEntry e = zipInputStreamObj.getNextEntry();
    e.getName // and all data
    int size = e.getSize(); // the byte count
    while (size > 0) {
       size -= zipInputStreamObj.read(...);
    }
    zipInputStreamObj.closeEntry();
    // DO THIS END
    
    zipInputStreamObj.close();
    

    注: 我不知道ZipInputStream.getNextEntry文件()在到达或未到达zip文件结尾时返回null。我希望如此,因为我不知道其他方法来实现时,没有更多的条目。

        2
  •  7
  •   Christian Schlichtherle    15 年前

    new FileOutputStream("/path/to/some-jar.jar/internal/zip/file.zip/myfile.txt");
    

    项目地点: http://truezip.java.net/ (已编辑)

        3
  •  0
  •   Tilak Sharma    13 年前

    我已经修改了上面提供的顺序邮政编码:

    File destFile = new File(destDir, jarName);
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));
    
    JarInputStream jis = new JarInputStream(is);
    JarEntry jarEntry = jis.getNextJarEntry();
    for (; jarEntry != null ; jarEntry = jis.getNextJarEntry()) {
        jos.putNextEntry(new JarEntry(jarEntry.getName()));
        if(jarEntry.isDirectory()) {
           continue;
        }
    
        int bytesRead = jis.read(buffer);
        while(bytesRead != -1) {
        jos.write(buffer, 0, bytesRead);
        bytesRead = jis.read(buffer);
        }
    
    }
    is.close();
    jis.close();
    jos.flush();
    jos.closeEntry();
    jos.close();
    

        4
  •  0
  •   Ben    13 年前

    也可以解析字符串,在另一个ZipInputStream上打开一个ZipInputStream,并将条目设置为其中的文件。

    e、 你有上面的字符串“path/to/some”-jar.jar/internal/zip/file文件.zip文件/myfile.txt文件"

    private static final String[] zipFiles = new String[] { ".zip", ".jar" };
    
    public static InputStream getResourceAsStream(final String ref) throws IOException {
        String abstractPath = ref.replace("\\", "/");
        if (abstractPath.startsWith("/")) {
            abstractPath = abstractPath.substring(1);
        }
        final String[] pathElements = abstractPath.split("/");
        return getResourceAsStream(null, pathElements);
    }
    
    private static InputStream getResourceAsStream(final ZipInputStream parentStream, final String[] pathElements)
            throws IOException {
    
        if (pathElements.length == 0) return parentStream;
    
        final StringBuilder nextFile = new StringBuilder();
        for (int index = 0; index < pathElements.length; index++) {
            final String pathElement = pathElements[index];
            nextFile.append((index > 0 ? "/" : "") + pathElement);
            if (pathElement.contains(".")) {
                final String path = nextFile.toString();
                if (checkForZip(pathElement)) {
                    final String[] restPath = new String[pathElements.length - index - 1];
                    System.arraycopy(pathElements, index + 1, restPath, 0, restPath.length);
                    if (parentStream != null) {
                        setZipToEntry(parentStream, path);
                        return getResourceAsStream(new ZipInputStream(parentStream), restPath);
                    } else return getResourceAsStream(new ZipInputStream(new FileInputStream(path)), restPath);
                } else {
                    if (parentStream != null) {
                        setZipToEntry(parentStream, path);
                        return parentStream;
                    } else return new FileInputStream(path);
                }
            }
        }
        throw new FileNotFoundException("File not found: " + nextFile.toString());
    }
    
    private static void setZipToEntry(final ZipInputStream in, final String name) throws IOException {
        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            if (entry.getName().equals(name)) return;
        }
        throw new FileNotFoundException("File not found: " + name);
    }
    
    private static boolean checkForZip(final String ref) {
        for (final String zipFile : zipFiles) {
            if (ref.endsWith(zipFile)) return true;
        }
        return false;
    }