01: /* *****************************************************************************
02: unzip a zlib compressed file
03: * ****************************************************************************/
04:
05: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
06: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
07: * Use is subject to license terms. *
08: * J_LZ_COPYRIGHT_END *********************************************************/
09:
10: package org.openlaszlo.test;
11:
12: import java.io.*;
13: import java.util.zip.*;
14:
15: public class unzip {
16:
17: public static int BUFFER_SIZE = 10240;
18:
19: public static int send(InputStream input, OutputStream output,
20: int size) throws IOException {
21: int c = 0;
22: byte[] buffer = new byte[size];
23: int b = 0;
24: while ((b = input.read(buffer)) > 0) {
25: c += b;
26: output.write(buffer, 0, b);
27: }
28: return c;
29: }
30:
31: public static int send(InputStream input, OutputStream output)
32: throws IOException {
33:
34: int available = input.available();
35: int bsize;
36: if (available == 0) {
37: bsize = BUFFER_SIZE;
38: } else {
39: bsize = Math.min(input.available(), BUFFER_SIZE);
40: }
41: return send(input, output, bsize);
42: }
43:
44: public static byte[] readFileBytes(InputStream istr, int skip)
45: throws IOException {
46: byte bytes[] = new byte[istr.available()];
47: System.err.println("read " + istr.available()
48: + " bytes from input stream");
49: istr.read(bytes);
50: istr.close();
51:
52: byte bytes2[] = new byte[bytes.length - skip];
53:
54: System.arraycopy(bytes, skip, bytes2, 0, bytes2.length);
55: return bytes2;
56: }
57:
58: static public void main(String args[]) {
59: try {
60: byte compressedData[] = readFileBytes(System.in, 8);
61:
62: // Create the decompressor and give it the data to compress
63: Inflater decompressor = new Inflater();
64: decompressor.setInput(compressedData);
65:
66: // Create an expandable byte array to hold the decompressed data
67: ByteArrayOutputStream bos = new ByteArrayOutputStream(
68: compressedData.length);
69:
70: // Decompress the data
71: byte[] buf = new byte[1024];
72: while (!decompressor.finished()) {
73: try {
74: int count = decompressor.inflate(buf);
75: bos.write(buf, 0, count);
76: } catch (DataFormatException e) {
77: }
78: }
79: try {
80: bos.close();
81: } catch (IOException e) {
82: }
83:
84: // Get the decompressed data
85: byte[] decompressedData = bos.toByteArray();
86: send(new ByteArrayInputStream(decompressedData), System.out);
87: } catch (Exception e) {
88: e.printStackTrace();
89: }
90: }
91: }
|