2012-02-15 215 views
3

以下是试图以gzip格式将数据存储在表“TESTBYTEA”中的java代码。我正在使用postgresql数据库。 TESTBYTEA表有一列BYTEA类型的“数据”列。我想压缩数据并存储它。从数据库读取数据时,我想解压并读取它。但是我收到了一个异常“不是GZIP格式”。我们可以将数据以gzip格式存储在postgresql中

public static void main(String[] args){ 
    insertBytes(connection);   
    readBytes(connection); 
    connection.close(); 
} 

public static void insertBytes(Connection connection) throws FileNotFoundException, IOException, SQLException{ 
    File file = new File("C:test.txt"); 
    FileReader fileReader = new FileReader(file); 
    char[] cbuf = new char[2000]; 
    int read = fileReader.read(cbuf); 
    String str = new String (cbuf); 
    byte[] bytes = gzip(str); 
    Statement statement = connection.createStatement(); 
    int result = statement.executeUpdate("INSERT INTO TESTBYTEA (data) VALUES ('\\\\x"+bytes+"')"); 
    System.out.println(result); 
} 


public static void readBytes(Connection connection) throws SQLException, IOException{ 
    Statement statement = connection.createStatement(); 
    ResultSet rs = statement.executeQuery("select data from testbytea"); 
    while(rs.next()){ 
     byte[] bs = rs.getBytes(1); 
     String str = gunzip(bs); 
     System.out.println(str); 
    } 
} 


private static String gunzip(byte[] bytes) throws IOException { 
    Reader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(bytes)), "US-ASCII"); 
    StringBuffer sbuf = new StringBuffer(); 

    char[] buffer = new char[32 * 1024]; 
    int nread; 
    while ((nread = reader.read(buffer)) >= 0) { 
     sbuf.append(buffer, 0, nread); 
    } 

    String s = sbuf.toString(); 

    reader.close(); 
    return s; 
} 

private static byte[] gzip(String s) throws IOException { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    GZIPOutputStream gzos = new GZIPOutputStream(baos); 
    Writer writer = new OutputStreamWriter(gzos, "US-ASCII"); 

    writer.write(s); 
    writer.flush(); 
    gzos.finish(); 

    byte[] bytes = baos.toByteArray(); 

    writer.close(); 
    return bytes; 
} 

但我收到以下异常

Exception in thread "main" 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) 
    at postgresjdbc.PostgresJDBC.gunzip(PostgresJDBC.java:237) 
    at postgresjdbc.PostgresJDBC.readBytes(PostgresJDBC.java:230) 
    at postgresjdbc.PostgresJDBC.main(PostgresJDBC.java:208) 
Java Result: 1 

任何有关此帮助表示赞赏。

+0

BTW自动PostgreSQL的压缩是足够大的存储外的线 – araqnid 2012-02-15 20:34:50

回答

3

你的问题是你如何插入字节:

"INSERT INTO TESTBYTEA (data) VALUES ('\\\\x"+bytes+"')" 

会产生类似

INSERT INTO TESTBYTEA (data) VALUES ('\\x[[email protected]') 

(基本上是byte[]引用在.toString()用于您的字节数组

你为什么不使用准备好的声明?

PreparedStatement pstmt = connection.prepareStatement(
    "INSERT INTO TESTBYTEA (data) VALUES (?)"); 
pstmt.setBytes(1, bytes); 
pstmt.executeUpdate(); 

编辑:

不要忘记:

pstmt.close(); 
+1

这解决了我的问题文本值。万分感谢。 – 2012-02-16 09:54:40

相关问题