Gzip 压缩和 http post 从 python 到 java

Gzip compression and http post from python to java

我想通过 http post 从 python 到 java 的 gzip 压缩数据,我想将其作为 BLOB 存储在数据库中。然后我想 gzip 解压缩 java 中的那个 BLOB。所以我想知道如何 post python 中的 BLOB 以及如何读取 java 中的 BLOB。我在下面给出了我的 python 和 java 代码。在我的代码中,我 gzip 压缩 python 中的一个字符串并将压缩后的数据存储在一个文件中。然后我在 java 中读取该文件并使用 GZIPInputStream 将其解压缩。但我收到以下异常。

java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:154)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:75)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:85)
    at GZipFile.gunzipIt(GZipFile.java:60)
    at GZipFile.main(GZipFile.java:43)

如果我在 python 中打印压缩数据的字节数组,我会得到

[31, 139, 8, 0, 254, 213, 186, 87, 2, 255, 203, 72, 205, 201, 201, 231, 229, 42, 207, 47, 202, 73, 1, 0, 66, 102, 86, 48, 12, 0, 0, 0]

如果我从 java 中的那个文件中读取并打印压缩数据,我会得到

[31, -17, -65, -67, 8, 0, -17, -65, -67, -42, -70, 87, 2, -17, -65, -67, - 17, -65, -67, 72, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, - 65, -67, 42, -17, -65, -67, 47, -17, -65, -67, 73, 1, 0, 66, 102, 86, 48, 12, 0, 0, 0]

有没有看出区别。如果我将 python 中打印的字节数组作为 java 代码的输入,它工作正常。因此,请帮助我了解如何 post python 中的 blob(压缩数据)以及如何读取 java 中的压缩数据以将其解压缩。

这是python中的压缩代码:

import StringIO  
import gzip  
import base64  
import os  


m='hello'+'\r\n'+'world'  

out = StringIO.StringIO()  
with gzip.GzipFile(fileobj=out, mode="wb") as f:  

    f.write(m.encode('utf-8'))
print list(array.array('B',out.getvalue())[:])
f=open('comp_dump','wb')  
f.write(out.getvalue())  
f.close()

这是java中的解压代码:

//$Id$

import java.io.*;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.zip.GZIPInputStream;  
import javax.xml.bind.DatatypeConverter;  
import java.util.Arrays;

public class GZipFile
{


public static String readCompressedData()throws Exception
{
        String compressedStr ="";
        String nextLine;
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("comp_dump")));
        try
        {
                while((nextLine=reader.readLine())!=null)
                {
                        compressedStr += nextLine;
                }
        }
        finally
        {
                reader.close();
        }
        return compressedStr;
}

public static void main( String[] args ) throws Exception
{
        GZipFile gZip = new GZipFile();
        byte[] contentInBytes = readCompressedData().getBytes("UTF-8");

        System.out.println(Arrays.toString(contentInBytes));
        String decomp = gZip.gunzipIt(contentInBytes);
        System.out.println(decomp);
}

/**
 * GunZip it
 */
public static String gunzipIt(final byte[] compressed){

        byte[] buffer = new byte[1024];
        StringBuilder decomp = new StringBuilder() ;

        try{

                GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(compressed));

                int len;
                while ((len = gzis.read(buffer)) > 0) {

                        decomp.append(new String(buffer, 0, len));

                }

                gzis.close();

        }catch(IOException ex){
                ex.printStackTrace();
        }
        return decomp.toString();
}
}

你检查过这个吗:gzip a file in Python ?

我猜你的字符串

m='hello'+'\r\n'+'world' 

可能会导致整个过程出现一些问题...

您是否考虑过使用双引号将其替换为 m="hello\r\nworld"?

您无法将压缩数据读取为字符串 directly.What 您在 readCompressedData 方法中所做的是将压缩数据读取为文字(这会导致错误的字符串),然后获取它的字节(在方法 main 中)。执行此操作后,contentInBytes 并不是真正存储在文件中的字节。

当您尝试使用无法转换为 String 的字节创建字符串时。表示字符串的字节不同。

例如:

        byte bytesBefore[] = {-1,-2,65,76,79,80};
        try {
            String str = new String(bytesBefore);
            byte bytesAfter[] = str.getBytes();
            System.out.println("str is " + str);
            System.out.println("after");
            for(Byte b : bytesAfter){
                System.out.print(" " + b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

输出:

str is ��ALOP
after
 -17 -65 -67 -17 -65 -67 65 76 79 80

因为这里的bytes -1和-2不能转成string,当你用bytesBefore new string时,str存入内存的bytes是bytesAfter,把-1和-2改成- 17 -65 -67 -17 -65 -67 .

实际上,GZIPInputStream可以用FileInputStream构建,不需要获取字节first.Just使用BufferedReader读取GZIPInputStream这是用 FileInputStream.

构建的

有解决办法:

import java.io.*;
import java.util.zip.GZIPInputStream;

public class GZipFile {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new GZIPInputStream(new FileInputStream(
                        "comp_dump")), "UTF-8"));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\r\n");
        }
        System.out.println(sb.toString());
    }
}

输出:

hello
world