001: // gzip.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: // last major change: 13.05.2004
007: //
008: // This program is free software; you can redistribute it and/or modify
009: // it under the terms of the GNU General Public License as published by
010: // the Free Software Foundation; either version 2 of the License, or
011: // (at your option) any later version.
012: //
013: // This program is distributed in the hope that it will be useful,
014: // but WITHOUT ANY WARRANTY; without even the implied warranty of
015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: // GNU General Public License for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // along with this program; if not, write to the Free Software
020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: //
022: // Using this software in any meaning (reading, learning, copying, compiling,
023: // running) means that you agree that the Author(s) is (are) not responsible
024: // for cost, loss of data or any harm that may be caused directly or indirectly
025: // by usage of this softare or this documentation. The usage of this software
026: // is on your own risk. The installation and usage (starting/running) of this
027: // software may allow other people or application to access your computer and
028: // any attached devices and is highly dependent on the configuration of the
029: // software which must be done by the user of the software; the author(s) is
030: // (are) also not responsible for proper configuration and usage of the
031: // software, even if provoked by documentation provided together with
032: // the software.
033: //
034: // Any changes to this file according to the GPL as documented in the file
035: // gpl.txt aside this file in the shipment you received can be done to the
036: // lines that follows this copyright notice here, but changes must not be
037: // done inside the copyright notive above. A re-distribution must contain
038: // the intact and unchanged copyright notice.
039: // Contributions and changes to the program code must be marked as such.
040:
041: package de.anomic.tools;
042:
043: import java.io.BufferedInputStream;
044: import java.io.BufferedOutputStream;
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.FileInputStream;
048: import java.io.FileNotFoundException;
049: import java.io.FileOutputStream;
050: import java.io.IOException;
051: import java.io.InputStream;
052: import java.io.OutputStream;
053: import java.util.zip.GZIPInputStream;
054: import java.util.zip.GZIPOutputStream;
055:
056: public class gzip {
057:
058: public static void gzipFile(String inFile, String outFile) {
059: try {
060: InputStream fin = new FileInputStream(inFile);
061: OutputStream fout = new GZIPOutputStream(
062: new FileOutputStream(outFile), 128);
063: copy(fout, fin, 128);
064: fin.close();
065: fout.close();
066: } catch (FileNotFoundException e) {
067: System.err
068: .println("ERROR: file '" + inFile + "' not found");
069: } catch (IOException e) {
070: System.err.println("ERROR: IO trouble");
071: }
072: }
073:
074: public static void gunzipFile(String inFile, String outFile) {
075: try {
076: InputStream fin = new GZIPInputStream(new FileInputStream(
077: inFile));
078: OutputStream fout = new FileOutputStream(outFile);
079: copy(fout, fin, 128);
080: fin.close();
081: fout.close();
082: } catch (FileNotFoundException e) {
083: System.err
084: .println("ERROR: file '" + inFile + "' not found");
085: } catch (IOException e) {
086: System.err.println("ERROR: IO trouble");
087: }
088: }
089:
090: public static byte[] gzipString(String in) {
091: try {
092: InputStream fin = new ByteArrayInputStream(in
093: .getBytes("UTF8"));
094: ByteArrayOutputStream baos = new ByteArrayOutputStream();
095: OutputStream fout = new GZIPOutputStream(baos, 128);
096: copy(fout, fin, 128);
097: fin.close();
098: fout.close();
099: return baos.toByteArray();
100: } catch (IOException e) {
101: System.err.println("ERROR: IO trouble");
102: return null;
103: }
104: }
105:
106: public static String gunzipString(byte[] in) {
107: try {
108: InputStream fin = new GZIPInputStream(
109: new ByteArrayInputStream(in));
110: ByteArrayOutputStream fout = new ByteArrayOutputStream();
111: copy(fout, fin, 128);
112: fin.close();
113: fout.close();
114: return new String(fout.toByteArray(), "UTF-8");
115: } catch (IOException e) {
116: System.err.println("ERROR: IO trouble");
117: return null;
118: }
119: }
120:
121: private static void copy(OutputStream out, InputStream in,
122: int bufferSize) throws IOException {
123: InputStream bIn = new BufferedInputStream(in, bufferSize);
124: OutputStream bOut = new BufferedOutputStream(out, bufferSize);
125: byte[] buf = new byte[bufferSize];
126: int n;
127: while ((n = bIn.read(buf)) > 0)
128: bOut.write(buf, 0, n);
129: bIn.close();
130: bOut.close();
131: }
132:
133: private static void help() {
134: System.out.println("AnomicGzip (2004) by Michael Christen");
135: System.out.println("usage: gzip [-u] <file> [<target-file>]");
136: }
137:
138: public static void main(String[] s) {
139: if (s.length == 0) {
140: help();
141: System.exit(0);
142: }
143: if ((s[0].equals("-h")) || (s[0].equals("-help"))) {
144: help();
145: System.exit(0);
146: }
147: if (s[0].equals("-u")) {
148: if ((s.length < 2) || (s.length > 3)) {
149: help();
150: System.exit(-1);
151: }
152: String target;
153: if (s.length == 2) {
154: if (s[1].endsWith(".gz"))
155: target = s[1].substring(0, s[1].length() - 3);
156: else
157: target = s[1] + ".gunzip";
158: } else {
159: target = s[2];
160: }
161: gzip.gunzipFile((s[1]), target);
162: System.exit(0);
163: }
164: if ((s.length < 1) || (s.length > 2)) {
165: help();
166: System.exit(-1);
167: }
168: String target;
169: if (s.length == 1)
170: target = s[0] + ".gz";
171: else
172: target = s[1];
173: gzip.gzipFile((s[0]), target);
174: System.exit(0);
175: }
176:
177: }
|