001: /*
002: * Copyright (c) 2001 Matthew Feldt. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided the copyright notice above is
006: * retained.
007: *
008: * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR
009: * IMPLIED WARRANTIES.
010: */
011:
012: /**
013: * DirectoryList.java
014: *
015: * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan
016: * Exercise 3-6:
017: * Write a program named Uncompress that uncompresses files and directories
018: * compressed by the Compress example in this chapter.
019: *
020: * @author Matthew Feldt <matthew@feldt.com>
021: * @version 1.0, 02/08/2001 22:08
022: */package org.openuss.utility;
023:
024: import java.io.*;
025:
026: import java.util.zip.*;
027:
028: public class Uncompress {
029: /**
030: * Use gunzip the string.
031: */
032: public static void gunzipFile(String from, String to)
033: throws IllegalArgumentException, IOException {
034: byte[] buffer = new byte[4096];
035: int bytes_read;
036:
037: // check input file
038: File fin = new File(from);
039:
040: if (!fin.exists()) {
041: throw new IllegalArgumentException(from
042: + " does not exist.");
043: }
044:
045: if (!fin.canRead()) {
046: throw new IllegalArgumentException(from
047: + " read protected.");
048: }
049:
050: // GZIPInputStream in = new GZIPInputStream(fileInputStream);
051: GZIPInputStream in = new GZIPInputStream(new FileInputStream(
052: from));
053:
054: File fout = new File(to);
055:
056: if (fout.exists()) {
057: // don't overwrite existing files
058: throw new IllegalArgumentException("File '" + to
059: + "' already exisits.");
060: }
061:
062: FileOutputStream out = new FileOutputStream(to);
063:
064: while ((bytes_read = in.read(buffer)) != -1)
065:
066: // write file
067: out.write(buffer, 0, bytes_read);
068:
069: in.close();
070: out.close();
071: }
072:
073: /**
074: * Use unzip the string.
075: */
076: public static void unzipFile(String from, String to)
077: throws IllegalArgumentException, IOException {
078: byte[] buffer = new byte[4096];
079: int bytes_read;
080:
081: // check input file
082: File fin = new File(from);
083:
084: if (!fin.exists()) {
085: throw new IllegalArgumentException(from
086: + " does not exist.");
087: }
088:
089: if (!fin.canRead()) {
090: throw new IllegalArgumentException(from
091: + " read protected.");
092: }
093:
094: ZipInputStream in = new ZipInputStream(
095: new FileInputStream(from));
096: ZipEntry entry;
097: FileOutputStream out;
098:
099: while ((entry = in.getNextEntry()) != null) {
100: String toName = to + File.separator + entry.getName();
101: File fout = new File(toName);
102:
103: /*
104: * This must be removed
105: *
106:
107: if (fout.exists()) { // don't overwrite existing files and continue
108: System.err.println("File '" + toName + "' already exisits.");
109: continue;
110: }
111:
112: * This must be insert
113: */
114: if (entry.isDirectory()) {
115: // create directory for directory entries
116: if (!fout.mkdirs()) {
117: System.err.println("Unable to create directory: "
118: + toName);
119: }
120: } else {
121: // write file
122: out = new FileOutputStream(toName);
123:
124: while ((bytes_read = in.read(buffer)) != -1)
125: out.write(buffer, 0, bytes_read);
126:
127: out.close();
128: }
129:
130: // System.out.println(toName);
131: }
132:
133: in.close();
134: }
135: }
|