Question

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.

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:

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.

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

Thanks in advance!

Was it helpful?

Solution

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:

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

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

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top