如何从java中的jar / zip文件中查找特定目录?

时间:2023-01-27 11:07:08

I have been working on this for quite a few hours. I can't seem to find the issue to this problem. Essentially what I have is this:

我一直在研究这个问题好几个小时。我似乎无法找到这个问题的问题。基本上我拥有的是这个:

I have a jar, let's call it "a.jar"

我有一个罐子,我们称它为“a.jar”

I need to get the directory "z" and it's contents from "a.jar", but "z" isn't in the root directory of "a.jar".

我需要从“a.jar”获取目录“z”及其内容,但“z”不在“a.jar”的根目录中。

"z" is in "/x/y/" and "/x/y/" is in "a.jar", so it looks like this:

“z”在“/ x / y /”中,“/ x / y /”在“a.jar”中,所以它看起来像这样:

"a.jar/x/y/z/"

I hope that's a decent explanation. By the way, "a.jar" is what everything is running out of, so its in the class path obviously.

我希望这是一个不错的解释。顺便说一句,“a.jar”就是一切都在用尽,所以它在课堂上的路径显而易见。

2 个解决方案

#1


3  

Basically for each ZipEntry you have to check if it isDirectory() and parse that also. Checkout this link: http://www.javaworld.com/javaworld/javatips/jw-javatip49.html

基本上对于每个ZipEntry,你必须检查它是否是目录()并解析它。查看此链接:http://www.javaworld.com/javaworld/javatips/jw-javatip49.html

LE:

Here is a complete example that extracts the files from the jar, and if you specify a specific path it will extract only that folder:

这是一个从jar中提取文件的完整示例,如果指定了特定路径,它将只提取该文件夹:

public void doUnzip(String inputZip, String destinationDirectory, String specificPath)
        throws IOException {
    int BUFFER = 2048;
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();

    ZipFile zipFile;
    // Open Zip file for reading
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration<?> zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        if(specificPath != null){
            if(entry.getName().startsWith(specificPath) == false)
                continue;
        }

        File destFile = new File(unzipDestinationDirectory, entry.getName());

        // create the parent directory structure if needed
        destFile.getParentFile().mkdirs();

        try {
            // extract file if not a directory
            if (!entry.isDirectory()) {
                BufferedInputStream is = new BufferedInputStream(
                        zipFile.getInputStream(entry));
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                        BUFFER);

                // read and write until last byte is encountered
                for (int bytesRead; (bytesRead = is.read(data, 0, BUFFER)) != -1;) {
                    dest.write(data, 0, bytesRead);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    zipFile.close();
}

public static void main(String[] args) {
    Unzip unzip = new Unzip();
    try {
        unzip.doUnzip("test.jar", "output", "x/y/z");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

#2


1  

..(ZipEntry), but they don't work very well with sub-directories.

..(ZipEntry),但它们与子目录不能很好地协同工作。

They work just fine. Iterate the entries and simply check the path equates to that sub-directory. If it does, add it to a list (or process it, whatever).

他们工作得很好。迭代条目,只需检查路径等于该子目录。如果是,请将其添加到列表中(或处理它,无论如何)。

#1


3  

Basically for each ZipEntry you have to check if it isDirectory() and parse that also. Checkout this link: http://www.javaworld.com/javaworld/javatips/jw-javatip49.html

基本上对于每个ZipEntry,你必须检查它是否是目录()并解析它。查看此链接:http://www.javaworld.com/javaworld/javatips/jw-javatip49.html

LE:

Here is a complete example that extracts the files from the jar, and if you specify a specific path it will extract only that folder:

这是一个从jar中提取文件的完整示例,如果指定了特定路径,它将只提取该文件夹:

public void doUnzip(String inputZip, String destinationDirectory, String specificPath)
        throws IOException {
    int BUFFER = 2048;
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();

    ZipFile zipFile;
    // Open Zip file for reading
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration<?> zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        if(specificPath != null){
            if(entry.getName().startsWith(specificPath) == false)
                continue;
        }

        File destFile = new File(unzipDestinationDirectory, entry.getName());

        // create the parent directory structure if needed
        destFile.getParentFile().mkdirs();

        try {
            // extract file if not a directory
            if (!entry.isDirectory()) {
                BufferedInputStream is = new BufferedInputStream(
                        zipFile.getInputStream(entry));
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                        BUFFER);

                // read and write until last byte is encountered
                for (int bytesRead; (bytesRead = is.read(data, 0, BUFFER)) != -1;) {
                    dest.write(data, 0, bytesRead);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    zipFile.close();
}

public static void main(String[] args) {
    Unzip unzip = new Unzip();
    try {
        unzip.doUnzip("test.jar", "output", "x/y/z");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

#2


1  

..(ZipEntry), but they don't work very well with sub-directories.

..(ZipEntry),但它们与子目录不能很好地协同工作。

They work just fine. Iterate the entries and simply check the path equates to that sub-directory. If it does, add it to a list (or process it, whatever).

他们工作得很好。迭代条目,只需检查路径等于该子目录。如果是,请将其添加到列表中(或处理它,无论如何)。