| | 45 | 再来看看读取的方法 |
| | 46 | public byte[] getContent() { |
| | 47 | if (!zip) { |
| | 48 | return content; |
| | 49 | } else {//只要数据库标识是GZIP的就用GZIP解压读取 |
| | 50 | byte[] result = null; |
| | 51 | ByteArrayInputStream bis = null; |
| | 52 | GZIPInputStream gzip = null; |
| | 53 | ByteArrayOutputStream baos = null; |
| | 54 | try { |
| | 55 | bis = new ByteArrayInputStream(content); |
| | 56 | gzip = new GZIPInputStream(bis); |
| | 57 | byte[] buf = new byte[1024]; |
| | 58 | int num = -1; |
| | 59 | baos = new ByteArrayOutputStream(); |
| | 60 | while ((num = gzip.read(buf, 0, buf.length)) != -1) { |
| | 61 | baos.write(buf, 0, num); |
| | 62 | } |
| | 63 | baos.flush(); |
| | 64 | result = baos.toByteArray(); |
| | 65 | } catch (Exception ex) { |
| | 66 | throw new RuntimeException(ex); |
| | 67 | } finally { |
| | 68 | try { |
| | 69 | if (gzip != null) { |
| | 70 | gzip.close(); |
| | 71 | } |
| | 72 | if (bis != null) { |
| | 73 | bis.close(); |
| | 74 | } |
| | 75 | if (baos != null) { |
| | 76 | baos.close(); |
| | 77 | } |
| | 78 | } catch (Exception e) { |
| | 79 | } |
| | 80 | } |
| | 81 | return result; |
| | 82 | } |
| | 83 | } |