Search code examples
javazipunzipmainframepkzip

How to unzip file zipped by PKZIP in mainframe by Java?


I am trying to write a program in Java to unzip files zipped by PKZIP tool in Mainframe. However, I have tried below 3 ways, none of them can solve my problem.

  1. By exe.

    I have tried to open it by WinRAR, 7Zip and Linux command(unzip). All are failed with below error message :

    The archive is either in unknown format or damaged

  2. By JDK API - java.util.ZipFile

    I also have tried to unzip it by JDK API, as this website described. However, it fails with error message :

    IO Error: java.util.zip.ZipException: error in opening zip file

  3. By Zip4J

    I also have tried to use Zip4J. It failed too, with error message :

    Caused by: java.io.IOException: Negative seek offset at java.io.RandomAccessFile.seek(Native Method) at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:117) ... 5 more

May I ask if there is any java lib or linux command can extract zip file zipped by PKZIP in Mainframe? Thanks a lot!


Solution

  • I have successfully read files that were compressed with PKZip on z/OS and transferred to Linux. I was able to read them with java.util.zip* classes:

            ZipFile ifile = new ZipFile(inFileName);
            // faster to loop through entries than open the zip file as a stream
            Enumeration<? extends ZipEntry> entries = ifile.entries();  
    
            while ( entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {  // skip directories
                    String entryName = entry.getName();
                    // code to determine to process omitted
                    InputStream zis = ifile.getInputStream(entry); 
                    // process the stream
                }
            }
    

    The jar file format is just a zip file, so the "jar" command can also read such files.

    Like the others, I suspect that maybe the file was not transferred in binary and so was corrupted. On Linux you can use the xxd utility (piped through head) to dump the first few bytes to see if it looks like a zip file:

    # xxd myfile.zip | head
    0000000: 504b 0304 2d00 0000 0800 2c66 a348 eb5e  PK..-.....,f.H.^
    

    The first 4 bytes should be as shown. See also the Wikipedia entry for zip files

    Even if the first 4 bytes are correct, if the file was truncated during transmission that could also cause the corrupt file message.