使用Java时解压缩存档将无效,适用于终端 - Linux

时间:2022-04-19 03:41:28

I'm trying to unpack/extract an archive, which is supplied in my program, containing binaries. The copy from within the jar to the file works just fine, but when I try to extract the zip, it returns unexpectedly and only copies half of a file, and ignores the other file completely.

我正在尝试解压缩/提取包含二进制文件的程序中提供的存档。从jar到文件的副本工作正常,但是当我尝试提取zip时,它会意外返回并且仅复制文件的一半,并完全忽略另一个文件。

Here's a bit more detailed description: I'm trying to unzip an archive copied to a folder, from within the program's .jar. The program I'm using to unzip is "unzip" (comes with Linux). The command used to extract is:

这里有一个更详细的描述:我试图从程序的.jar中解压缩复制到文件夹的存档。我用来解压缩的程序是“解压缩”(随Linux一起提供)。用于提取的命令是:

unzip -o <file>.zip

which is exactly what I'm using in following code:

这正是我在下面的代码中使用的:

ProcessBuilder process = new ProcessBuilder();
process.command("unzip", "-o", adb_tools.toString());
process.redirectErrorStream(true);
Process pr = process.start();
String line;
BufferedReader processReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line = processReader.readLine()) != null) 
    log(Level.INFO, "Extracting Android Debugging Bridge: " + line, true);

log(Level.INFO, "Android Debugging Bridge has been extracted and installed to system. Marking files as executable...", true);
pr.destroy();
processReader.close();

When I use the command directly via the Terminal, everything works fine, both files are extracted and inflated, and are executable, however, as mentioned above, when I use the command in Java, only one file gets copied (and even that only goes half way), and the other file is completely ignored.

当我直接通过终端使用命令时,一切正常,两个文件都被提取和膨胀,并且是可执行的,但是,如上所述,当我在Java中使用命令时,只复制一个文件(甚至只会复制一个文件)中途),另一个文件完全被忽略。

How can I fix this problem, and prevent this happening again, with different programs?

我如何解决这个问题,并使用不同的程序再次防止这种情况发生?

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

If you need to do a common task in Java, there is always a library out there which does what you need better than yourself. So use an external library for unzipping. Check here:

如果您需要在Java中执行常见任务,那么总会有一个库可以比您自己更好地完成所需的操作。因此,使用外部库进行解压缩。点击这里:

What is a good Java library to zip/unzip files?

压缩/解压缩文件的优秀Java库是什么?

It looks like you can use zip4j like this (from djangofan's answer):

看起来你可以像这样使用zip4j(来自djangofan的回答):

public static void unzip(){
    String source = "some/compressed/file.zip";
    String destination = "some/destination/folder";
    String password = "password";

    try {
         ZipFile zipFile = new ZipFile(source);
         if (zipFile.isEncrypted()) {
            zipFile.setPassword(password);
         }
         zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }
}

#1


1  

If you need to do a common task in Java, there is always a library out there which does what you need better than yourself. So use an external library for unzipping. Check here:

如果您需要在Java中执行常见任务,那么总会有一个库可以比您自己更好地完成所需的操作。因此,使用外部库进行解压缩。点击这里:

What is a good Java library to zip/unzip files?

压缩/解压缩文件的优秀Java库是什么?

It looks like you can use zip4j like this (from djangofan's answer):

看起来你可以像这样使用zip4j(来自djangofan的回答):

public static void unzip(){
    String source = "some/compressed/file.zip";
    String destination = "some/destination/folder";
    String password = "password";

    try {
         ZipFile zipFile = new ZipFile(source);
         if (zipFile.isEncrypted()) {
            zipFile.setPassword(password);
         }
         zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }
}