1

I am having two application one written in java where required to zip string of data and other in golang and required to unzip the record zipped by first application

Java program to Creating Zipped of string data

public static byte[] createZipForLicenses(String string) throws UnsupportedEncodingException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    zipOutputStream.setLevel(Deflater.DEFAULT_COMPRESSION);

    try {

        if (string != null && string.length() > 0) {
            ZipEntry zipEntry = new ZipEntry("data");
            zipOutputStream.putNextEntry(zipEntry);
            zipOutputStream.write(string.getBytes("UTF-8"));
            zipOutputStream.closeEntry();
        }

        zipOutputStream.close();

    } catch (IOException e) {

    }
    return outputStream.toByteArray();
}

Golang program to unzip the string data

func Unzip(data []byte) (string, error) {

rdata := bytes.NewReader(data)
r, err := zlib.NewReader(rdata) //**Error**-> "zlib: invalid header 
if err != nil {
    return "", err
}
s, err := io.ReadAll(r)
if err != nil {
    return "", err
}
return string(s), nil

}

I tried using compress/flate lib also but with this getting error "flate: corrupt input before offset 5"

6
  • 1
    Did you try to open the zip file with a 3rd party application to test whether writing or reading is the problem? Commented Nov 3, 2022 at 7:27
  • 3
    In Go package compress/zlib is for reading zlib compressed data. Reading zipfiles is done via archive/zip. Always read the package doc of any package you use. Commented Nov 3, 2022 at 8:35
  • In golang I have used with two packages compress/flate and compress/zlib with zlib to unzip I am getting the error:- "zlib: invalid header" with flate to unip I am getting the error:- "flate: corrupt input before offset 5" Commented Nov 3, 2022 at 8:47
  • 1
    Please listen closely: Neither compress/zlib nor compress/flate has anything to do with zipfiles. Really. Zipfiles are manipulated with archive/zip only. Commented Nov 3, 2022 at 9:18
  • Kindly not the program above, using java we zip(compress) the string data and produces zipped value in form of byte array and then again unzipping the same zipped record using golang then getting the error Commented Nov 3, 2022 at 9:36

2 Answers 2

1
func Unzip(data []byte) (string, error) {
zipReader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
    panic(err)
}
if len(zipReader.File) == 0 {
    return "", nil // No file to open / extract
}
f, err := zipReader.File[0].Open()
if err != nil {
    panic(err)
}
p, err := ioutil.ReadAll(f)
if err != nil {
    return "", err
}
return string(p), nil

}

Sign up to request clarification or add additional context in comments.

Comments

-1

Java part looks good to me:

public static byte[] createZipForLicenses(String string) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(out);
    zipOutputStream.setLevel(Deflater.DEFAULT_COMPRESSION);

    if (string != null && string.length() > 0) {
        ZipEntry zipEntry = new ZipEntry("data");
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.write(string.getBytes("UTF-8"));
        zipOutputStream.closeEntry();
    }

    zipOutputStream.close();

    return out.toByteArray();
}

And am able to read this zip with WinZip. And this is a zip info:

(PK0506) End of Central directory record
========================================
    - location:                                     116 (0x00000074) bytes
    - size:                                         22 bytes
    part number of this part (0000):                1
    part number of start of central dir (0000):     1
    number of entries in central dir in this part:  1
    total number of entries in central dir:         1
    size of central dir:                            50 (0x00000032) bytes
    relative offset of central dir:                 66 (0x00000042) bytes
    zipfile comment length:                         0 bytes

(PK0102) Central directory
==========================
    - location:                                     66 (0x00000042) bytes
    - size:                                         54 bytes
    total entries:                                  1

#1 (PK0102) [UTF-8] data
------------------------
    - location:                                     66 (0x00000042) bytes
    - size:                                         50 bytes
    part number of this part (0000):                1
    relative offset of local header:                0 (0x00000000) bytes
    version made by operating system (00):          MS-DOS, OS/2, NT FAT
    version made by zip software (20):              2.0
    operat. system version needed to extract (00):  MS-DOS, OS/2, NT FAT
    unzip software version needed to extract (20):  2.0
    general purpose bit flag (0x0808) (bit 15..0):  0000.1000 0000.1000
      file security status  (bit 0):                not encrypted
      data descriptor       (bit 3):                yes
      strong encryption     (bit 6):                no
      UTF-8 names          (bit 11):                yes
    compression method (08):                        deflated
      compression sub-type (deflation):             normal
    file last modified on (0x5563 0x542A):          2022-11-03 10:33:20
    32-bit CRC value:                               0x373555E7
    compressed size:                                16 bytes
    uncompressed size:                              14 bytes
    length of filename:                             4
                                                    UTF-8
    64 61 74 61                                     data
    length of file comment:                         0 bytes
    internal file attributes:                       0x0000
      apparent file type:                           binary
    external file attributes:                       0x00000000
      WINDOWS   (0x00):                             none
      POSIX (0x000000):                             ?---------

(PK0304) ZIP entries
====================
    total entries:                                  1

#1 (PK0304) [UTF-8] data
------------------------
    - location:                                     0 (0x00000000) bytes
    - size:                                         34 bytes
    operat. system version needed to extract (00):  MS-DOS, OS/2, NT FAT
    unzip software version needed to extract (20):  2.0
    general purpose bit flag (0x0808) (bit 15..0):  0000.1000 0000.1000
      file security status  (bit 0):                not encrypted
      data descriptor       (bit 3):                yes
      strong encryption     (bit 6):                no
      UTF-8 names          (bit 11):                yes
    compression method (08):                        deflated
      compression sub-type (deflation):             normal
    file last modified on (0x5563 0x542A):          2022-11-03 10:33:20
    32-bit CRC value:                               0x00000000
    compressed size:                                0 bytes
    uncompressed size:                              0 bytes
    length of filename:                             4
                                                    UTF-8
    64 61 74 61                                     data

#1 (PK0708) Data descriptor
---------------------------
    - location:                                     50 (0x00000032) bytes
    - size:                                         16 bytes
    32-bit CRC value:                               0x373555E7
    compressed size:                                16 bytes
    uncompressed size:                              14 bytes

Looks like you unzip it with Go incorrectly.

P.S. To avoid a lot of code when zip/unzip files in java, you can use zip4jvm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.