'不是Gzip格式的在Java中解压缩GZIP Inputstream时出错



我的代码正在尝试从GZPICPECT文件中解压缩流。

这是代码段:

   InputStream is = new GZIPInputStream(new ByteArrayInputStream(fcontents.getBytes()));

文件本身很好:

$cat storefront3.gz  | gunzip
180028796
80026920
180028796
180026921
8002790180
800001
1800002
1800007
800008
800009

通过FileInputStream在顶级代码段之前读取的数据肯定看起来像GZIP的东西(注意原始文件是Storefront3.tsv):

��[�Rstorefront3.tsvu���0k{)�?�/FBģ��Y'��Q�a���s~���}6���d�{2+���O���D�m~�O��

,但要获取以下内容:

Caused by: java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:141)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:56)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:65)

这是.gz文件的十六进制

23:40:44/storefronts:72 $od -cx  storefront3.gz
0000000  037 213  b  b 201   [ 347   R   003   s   t   o   r   e   f
             8b1f    0808    5b81    52e7    0300    7473    726f    6665
0000020    r   o   n   t   3   .   t   s   v     u 212 273 025 200   0
             6f72    746e    2e33    7374    0076    8a75    15bb    3080
0000040   f 003   k   { 032   ) 200   ? 373   /   F   B   ģ  ** 302 131
             030c    7b6b    291a    3f80    2ffb    4246    a3c4    cdc2
0000060    Y   ' 261 200   Q 331   a 276 276 350 001   s   ~ 222 262 175
             2759    80b1    d951    be61    e8be    7301    927e    dcb2
0000100    }   6 226 231 367   d 200   {   2   + 211 337 342 020   O 022
             367d    9996    64f7    7b80    2b32    df89    10e2    f14f
0000120  022 343 035 246   D 211   m   ~ 003 326   O 235 030 236    
             e312    a61d    8944    7e6d    d603    9d4f    9e18    0000
0000140   
             0000

update

我还尝试使用fileinputstream。以下给出相同的错误

      GZIPInputStream strm = new GZIPInputStream(new FileInputStream(tmpFileName));

由于 fcontents包含您的GZED数据,因此应该是byte[]而不是String

我建议使用ioutil将文件读取为字节数组,因为将其读取为字符串很可能会损坏您的数据。

最新更新