【问题标题】:Unable to unpack a .tar file inside a zip file.?无法在 zip 文件中解压 .tar 文件。?
【发布时间】:2016-03-28 04:51:14
【问题描述】:

我需要解压缩所有包含 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 文件。下面我提供了我的 sn-p。

代码从这里开始

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());
    }
 }
}
}

谁能帮我提取这个? 提前致谢 轨迹追踪: E:\test\26-03-2016\order\7930199_1.zip E:\test\26-03-2016\order\7930199_1\ItemFile\1458887416277_12.zip E:\test\26-03-2016\order\7930199_1\ItemFile\1458887416277_12\ftp\content- 提供者\ewh-e\data\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)

【问题讨论】:

  • 无法提取是什么意思?有什么错误吗?
  • 亲爱的 sidgate,感谢您的宝贵回复,是的,我收到了错误,提供了我的堆栈跟踪。
  • @sidgate : 请帮我解压 .tar 文件
  • @sidgate : 请帮我解压指定的 .tar 文件的所有文件。
  • @sidgate:先生,请帮帮我

标签: java zip extract tar


【解决方案1】:

这个问题之前已经在 SO 上回答过。如果问题已经存在,最好也搜索一下。

无论如何,这是一个非常相似的问题。似乎使用 Apache Commons Compress 是要走的路。

How do I extract a tar file in Java?

【讨论】:

  • 你能帮我在我的应用程序中实现这个吗?
  • 因为我不清楚使用哪个sn-p以及如何使用?
  • 请您指导我。
  • 请帮帮我先生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2011-11-30
相关资源
最近更新 更多