Proces Java - nie można rozpakować pliku zip
Próbuję rozpakować jakiś plik zip, ma około 65 megabajtów. Fragmenty kodu poniżej:
Ta metoda faktycznie rozpakowuje plik:
public synchronized void execute(Path zipFile) {
final ProcessBuilder builder = new ProcessBuilder("/bin/unzip",zipFile.toAbsolutePath().toString(), "-d", dir.toAbsolutePath().toString());
FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
@Override public Integer call() {
try {
System.out.println("started and waiting");
int status = builder.start().waitFor();
System.out.println("status: " + status);
return status;
} catch (InterruptedException e) {
} catch (IOException e) {
}
return 0;
}
});
List<FutureTask<Integer>> tasks = new ArrayList<FutureTask<Integer>>();
tasks.add(task);
System.out.println("task added");
ExecutorService executor = Executors.newCachedThreadPool();
for (FutureTask<Integer> t : tasks) {
executor.submit(t);
System.out.println("submitted");
}
executor.shutdown();
System.out.println("shutdown");
}
Ten executor / przyszłe rzeczy są tylko po to, by mieć pewność, że zrobię to poprawnie. Ta metoda jest wywoływana w Finderze klasy, który znajduje plik zip w katalogu i próbuje go rozpakować. Jest oparty na tym kodziehttp://docs.oracle.com/javase/tutorial/essential/io/walk.html
Tak więc być konkretnym:
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (find(file)) {
synchronized(Finder.class) {
executor.execute(file);
}
}
return CONTINUE;
}
Teraz problem. To naprawdę zabawne. Ilekroć wyciągam coś za pomocą tego kodu, plik zip jest rozpakowywany, ale TYLKO niektóre katalogi są rozpakowywane, a inne nie. Na przykład mam plik zip z katalogami temp foo i bar, ale po rozpakowaniu są tylko katalogi temp i foo, a pasek nie jest wyodrębniany.
Wynik tego jest:
task added
submitted
started and waiting
shutdown
Dlaczego nie ma wyjścia „status = coś”?
Nie mogę zrozumieć, dlaczego tak jest. Gdy rozpakuję go ręcznie, zostanie poprawnie rozpakowany.
// EDYTOWAĆ
to załatwiło sprawę
@Override
public synchronized void execute(String file, String dest) {
ProcessBuilder pb = new ProcessBuilder("/bin/unzip","-qq", file, "-d", dest);
pb.redirectErrorStream(true);
try {
Process p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader r = new InputStreamReader(is);
BufferedReader in = new BufferedReader(r);
String line;
while ((line = in.readLine()) != null) {
// because of the -qq option, it does actually write out nothing
System.out.println(line);
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}