3

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!

piet.t
  • 11,718
  • 21
  • 43
  • 52
Vincent
  • 33
  • 1
  • 5
  • 2
    If not even rar nor 7zip can open the file you have to assume that the file is indeed corrupt. – Heri Nov 12 '16 at 14:48
  • Where is your Java code executing (mainframe or other)? What were the zip options when the file was created on the mainframe? Are there code page issues? Are the first two bytes x'504B' (i.e. "PK" in "ASCII")? – cschneid Nov 12 '16 at 15:17
  • 3
    If you don't get it off Mainframe as a "binary" file, you will pickle it bad. Is your content in EBCDIC? – Bill Woodger Nov 12 '16 at 18:09
  • 2
    If you downloaded the file to the local machine using FTP, make 100% sure you did a binary transfer. Text will do code conversion from EBCDIC to ISO-8859, which will create all types of mayhem. Also, any EBCDIC text inside the compressed files will be EBCDIC, so conversion has to occur somewhere. – zarchasmpgmr Nov 13 '16 at 15:53
  • Seems the zip file is indeed corrupt due to not transfering it in binary, it is solved now, thank you all. – Vincent Nov 15 '16 at 07:18

1 Answers1

4

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.

randomScott
  • 306
  • 2
  • 8
  • Hi randomScott, you are correct. I found that the result of the xxd command is : "0000000: 262e 039c 9d00 0000 9700 e96f 7ca8 6a0d &..........o|.j. 0000010: bea5 9801 0000 8709 0000 1800 6d00 3c99 ............m.<." , which is not started from "PK..-.....,f.H.^". So I think the file is indeed corrupt as not transferred in binary from MainFrame to my linux server. – Vincent Nov 15 '16 at 06:49
  • @radomScott Is this a good data. 0000000: 504b 0304 1400 0600 0800 adb0 9a4e 5306 PK...........NS. 0000010: ee08 0108 1a2e 1fff fbda 1c00 0000 4943 ..............IC – loneStar May 15 '19 at 18:08