-2

疑問に思っていますが、Javaでzipファイルをどのように解凍しますか?

私が最初に試した:

String source = "forge.zip";
String destination = "some/destination/folder";
try {
    zipFile = new ZipFile(source);
    zipFile.extractAll(destination);
    } catch (ZipException e) {
     e.printStackTrace();
    }

これは、zpFileにextractAllメソッドがないことを示しています。本当?

4

1 に答える 1

1

あなたがどんな問題を抱えているのかわかりません。しかし、私はこれをZipInputStreamsとZipEntrysで数回行ったことがあり、これが機能することはわかっています。

File dir = new File(destDir);

if(!dir.exists()) dir.mkdirs();
FileInputStream fis;

byte[] buffer = new byte[1024];
try {
    fis = new FileInputStream(zipFilePath);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();

    while(ze != null){
        String fileName = ze.getName();
        File newFile = new File(destDir + File.separator + fileName);

        new File(newFile.getParent()).mkdirs();
        FileOutputStream fos = new FileOutputStream(newFile);
        int len;

        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        fos.close();
        zis.closeEntry();
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
    fis.close();

} catch (IOException e) {
    e.printStackTrace();
}
于 2013-02-06T23:29:54.277 に答える