在另一个线程中下载和解压缩文件,从而导致存档损坏

时间:2018-01-23 22:04:43

标签: java multithreading javafx download

我正在尝试从给定网址下载.zip存档并将其解压缩到选定位置。由于下载非常大,我尝试在不同的线程中进行,因此它不会冻结整个事情,但它并没有真正取得圆满成功...通常情况下,我会创建一个runnable和thread对象,但是现在,因为我使用的是javafx,所以它会产生一些错误。我在线搜索,我不得不使用Platform.runLater()。这是我的代码:

private void startTask(Label st, ListView<String> view, HashMap<String, String> hash)
{

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            runTask(st,view,hash);
        }
    });
}

private void runTask(Label st, ListView<String> view, HashMap<String, String> hash){
    String link = hash.get(view.getSelectionModel().getSelectedItem());
    File file = new File("temp.zip");
    try {
        FileUtils.copyURLToFile(new URL(link), file);
        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
        ZipEntry zipEntry = zis.getNextEntry();
        File newFile;
        FileOutputStream fos;
        String fileName;
        while(zipEntry != null){

            fileName = zipEntry.getName();
            if(fileName.contains("##TEMP##")) continue;
            if(fileName.contains("MACOSX")) continue;
            newFile = new File(fileName);
            fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            zipEntry = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    } catch (IOException | NullPointerException e) {
        e.printStackTrace();
    }
    st.setText("Status: Ready");


}

这是结果:39ko temp.zip文件似乎已损坏。它没有解压缩(显然)。我尝试了来自多个域的许多链接,仍然是同样的问题...

编辑:尝试了另一种方法......同样的错误..新代码:

        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        InputStream in = connection.getInputStream();
        FileOutputStream out = new FileOutputStream("download.zip");
        copy(in, out, 1024);
        out.close();
public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
    byte[] buf = new byte[bufferSize];
    int n = input.read(buf);
    while (n >= 0) {
        output.write(buf, 0, n);
        n = input.read(buf);
    }
    output.flush();
}

我评论了解压缩部分,仍然是相同的......结论:下载是个问题。

1 个答案:

答案 0 :(得分:1)

您可以在while循环的顶部执行continue检查zipEntry = zis.getNextEntry(),返回到while循环的开头。但 while(zipEntry != null) { fileName = zipEntry.getName(); if(fileName.contains("##TEMP##")) continue; if(fileName.contains("MACOSX")) continue; // unzipping code zipEntry = zis.getNextEntry(); // never happens when filename contains ##TEMP## or MACOSX } 位于循环的底部,因此在这些情况下不会调用它。它似乎陷入无限循环

getNextEntry()

ZipEntry zipEntry; while((zipEntry = zis.getNextEntry()) != null) { fileName = zipEntry.getName(); if(fileName.contains("##TEMP##")) continue; if(fileName.contains("MACOSX")) continue; // unzipping code } 代码移到循环的顶部,并从变量声明中删除第一个调用。

{{1}}