无法解压缩zip文件中的.tar文件。?

时间:2016-03-28 04:51:14

标签: java zip extract tar

我需要解压缩包含zip文件的所有文件。 但Zip文件中还包含一个.tar文件。 考虑以下路径: 原始ZIP路径:E:\ test \ 26-03-2016 \ order \ 7930199_1.zip 提取路径:E:\ test \ 26-03-2016 \ order \ 7930199_1 \ ItemFile \ 1458887416277_12 \ ftp \ content-providers \ ewh-e \ data \ incomingabp

          In the Extracted path ,The folder contains a .tar file as OBX00000000005442A.tar and it has several folders with in it.

我已提取到incomingabp,但我无法提取.tar文件。 请帮我提取这个.tar文件。我已经提供了我的代码片段。

代码从这里开始

static public void extractFolder(String zipFile) throws 
ZipException,     
 IOException 
{
System.out.println(zipFile);
int BUFFER = 2048;
File file = new File(zipFile);

ZipFile zip = new ZipFile(file);
String newPath = zipFile.substring(0, zipFile.length() - 4);

new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();

// Process each entry
while (zipFileEntries.hasMoreElements())
{
    // grab a zip file entry
    ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
    String currentEntry = entry.getName();
    File destFile = new File(newPath, currentEntry);
    //destFile = new File(newPath, destFile.getName());
    File destinationParent = destFile.getParentFile();

    // create the parent directory structure if needed
    destinationParent.mkdirs();

    if (!entry.isDirectory())
    {
        BufferedInputStream is = new BufferedInputStream(zip
        .getInputStream(entry));
        int currentByte;
        // establish buffer for writing file
        byte data[] = new byte[BUFFER];

        // write the current file to disk
        FileOutputStream fos = new FileOutputStream(destFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos,
        BUFFER);

        // read and write until last byte is encountered
        while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, currentByte);
        }
        dest.flush();
        dest.close();
        is.close();
    }

    if (currentEntry.endsWith(".zip")||currentEntry.endsWith(".tar"))
    {
        // found a zip file, try to open
        extractFolder(destFile.getAbsolutePath());
    }
 }
}
}

任何人都可以帮我提取这个吗? 提前致谢     Strack Trace:     E:\测试\ 26-03-2016 \命令\ 7930199_1.zip     E:\测试\ 26-03-2016 \命令\ 7930199_1 \ ItemFile \ 1458887416277_12.zip     E:\测试\ 26-03-2016 \命令\ 7930199_1 \ ItemFile \ 1458887416277_12 \ FTP \内容 -     提供商\ EWH-E \数据\ incomingabp \ OBX00000000005442A.tar

Exception in thread "main" java.util.zip.ZipException: error in opening  
zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at Extraction_ZIP.Extraction3.extractFolder(Extraction3.java:24)
at Extraction_ZIP.Extraction3.extractFolder(Extraction3.java:68)
at Extraction_ZIP.Extraction3.extractFolder(Extraction3.java:68)
at Execution_point.Cl_Execute.main(Cl_Execute.java:29)

1 个答案:

答案 0 :(得分:0)

这个问题在此之前就已经得到了回答。如果问题已经存在,那么搜索总是最好的。

无论如何,这是一个非常相似的问题。似乎使用Apache Commons Compress是可行的方法。

How do I extract a tar file in Java?