0

I need to unpack all the Files containing a zip file. But the Zip file also contains a .tar file in it. Consider the below path : Original ZIP path : E:\test\26-03-2016\order\7930199_1.zip Extracted path : 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.

I have extracted upto incomingabp,But I am not able to extract the .tar file. Kindly help me to extract this .tar file.Below I have provided my snippet.

code starts here

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

Can any one help me to extract this? Thanks in advance Strack Trace: 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- providers\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)

1 Answers1

0

This question has been answered before here on SO. It is always best too search if the question already exists.

Anyway, here is a very similar question. Seems like using Apache Commons Compress is the way to go.

How do I extract a tar file in Java?

Community
  • 1
  • 1
gustf
  • 1,959
  • 13
  • 20