简体   繁体   English

使用 java 程序创建 Zip 并在使用 Golang 解压缩时出现“zlib: invalid header”问题

[英]Create Zip with java program and while unzip using Golang, getting issue as "zlib: invalid header"

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 中编写,其中需要 zip 数据字符串,另一个在 golang 中,需要解压缩第一个应用程序压缩的记录

Java program to Creating Zipped of string data Java 创建压缩字符串数据的程序

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 Golang程序解压字符串数据

func Unzip(data []byte) (string, error) { 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"我也尝试使用 compress/flate lib,但出现此错误“flate: corrupt input before offset 5”

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

} }

Java part looks good to me: Java 部分对我来说不错:

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 .并且能够使用WinZip阅读此zip And this is a zip info:这是 zip 信息:

(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.看起来你用Go解压不正确。

PS To avoid a lot of code when zip/unzip files in java, you can use zip4jvm PS为了避免在 java 中压缩/解压缩文件时出现大量代码,您可以使用zip4jvm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM