title: Reading gzipped string in sqlite3 database (python write, Java Android read)
body: <p>I'm trying to minimize a sqlite3 db with lots of HTMLs by compression. I used python to create the sqlite3 db, and I'm trying to decompress correctly on Android.</p>
  
  <p>I use gzip to compress the HTMLs and stores in the db as BLOB. Here's the code I wrote for creating the sqlite3 db (in Python):</p>
  
  <pre><code>from sys import stdin, argv
  import sqlite3
  import gzip
  import cStringIO
  
  def compressBuf(buf):
      zbuf = cStringIO.StringIO()
      zfile = gzip.GzipFile(mode = 'wb',  fileobj = zbuf, compresslevel = 9)
      zfile.write(buf)
      zfile.close()
      return zbuf.getvalue()
  
  conn = sqlite3.connect(argv[1])
  conn.text_factory = str 
  c = conn.cursor()
  
  c.execute('''CREATE TABLE articles (
      id INTEGER NOT NULL PRIMARY KEY,
      name TEXT, category TEXT, html BLOB );''')
  
  c.execute(' CREATE INDEX name_index on articles (name); ')
  
  for line in stdin:
      line = line.strip().split('\t')
      line[-1] = sqlite3.Binary(compressBuf(line[-1]))    
  
      c.execute('INSERT INTO articles VALUES (?, ?, ?, ?);', line)
  
  conn.commit()
  c.close()
  conn.close()
  </code></pre>
  
  <p>Here's the code snippet for Android:</p>
  
  <pre><code>Cursor cursor = db.rawQuery("SELECT html FROM articles WHERE id = " + id + " limit 1;", null);
  cursor.moveToFirst();
  byte[] zhtml = cursor.getBlob(0);
  ByteArrayInputStream is = new ByteArrayInputStream(zhtml);
  GZIPInputStream gis = new GZIPInputStream(is, zhtml.length);
  </code></pre>
  
  <p>I'm getting the following exception complaining the header is incorrect: </p>
  
  <pre><code> java.io.IOException: unknown format (magic number 213c)
     at java.util.zip.GZIPInputStream.&lt;init&gt;(GZIPInputStream.java:84)
     at tw.cse.o0o.MyApp.WebServer$ArticleHandler$1.writeTo(WebServer.java:196)
     at org.apache.http.entity.EntityTemplate.writeTo(EntityTemplate.java:76)
     at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)
     at org.apache.http.impl.AbstractHttpServerConnection.sendResponseEntity(AbstractHttpServerConnection.java:182)
     at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:209)
     at tw.cse.o0o.MyApp.WebServer.run(SQLHelper.java:90)
  </code></pre>
  
  <p>Using the Python interpreter I can confirm the function compressBuf returns with the correct gzip magic number 0x1f8b:</p>
  
  <pre><code>&gt;&gt;&gt; compressBuf('test')
  '\x1f\x8b\x08\x00 \xba:O\x02\xff+I-.\x01\x00\x0c~\x7f\xd8\x04\x00\x00\x00'
  </code></pre>
  
  <p>[Edit]</p>
  
  <p>Okay, this is what I've found out:</p>
  
  <p>On Nexus One, I the getBlob() function automatically decompresses the binary data, whether if its zlib or gzip. The 213c in the error log is the first two character of the original html.
  However, this is not the case on Samsung Galaxy Tab (first gen). I'm still trying to find a way to decompress on my Galaxy Tab..</p>