KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > utility > Uncompress


1 /*
2  * Copyright (c) 2001 Matthew Feldt. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided the copyright notice above is
6  * retained.
7  *
8  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR
9  * IMPLIED WARRANTIES.
10  */

11
12 /**
13  * DirectoryList.java
14  *
15  * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan
16  * Exercise 3-6:
17  * Write a program named Uncompress that uncompresses files and directories
18  * compressed by the Compress example in this chapter.
19  *
20  * @author Matthew Feldt <matthew@feldt.com>
21  * @version 1.0, 02/08/2001 22:08
22  */

23
24 /*
25  * Some lines are changed for this application
26  */

27 package org.openuss.utility;
28
29 import java.io.*;
30
31 import java.util.zip.*;
32
33
34 public class Uncompress {
35     /**
36      * Use gunzip the string.
37      */

38     public static void gunzipFile(String JavaDoc from, String JavaDoc to)
39                            throws IllegalArgumentException JavaDoc, IOException {
40         byte[] buffer = new byte[4096];
41         int bytes_read;
42
43         // check input file
44
File fin = new File(from);
45
46         if (!fin.exists()) {
47             throw new IllegalArgumentException JavaDoc(from + " does not exist.");
48         }
49
50         if (!fin.canRead()) {
51             throw new IllegalArgumentException JavaDoc(from + " read protected.");
52         }
53
54         // GZIPInputStream in = new GZIPInputStream(fileInputStream);
55
GZIPInputStream in = new GZIPInputStream(new FileInputStream(from));
56
57         File fout = new File(to);
58
59         if (fout.exists()) {
60             // don't overwrite existing files
61
throw new IllegalArgumentException JavaDoc("File '" + to +
62                                                "' already exisits.");
63         }
64
65         FileOutputStream out = new FileOutputStream(to);
66
67         while ((bytes_read = in.read(buffer)) != -1)
68
69             // write file
70
out.write(buffer, 0, bytes_read);
71
72         in.close();
73         out.close();
74     }
75
76     /**
77      * Use unzip the string.
78      */

79     public static void unzipFile(String JavaDoc from, String JavaDoc to)
80                           throws IllegalArgumentException JavaDoc, IOException {
81         byte[] buffer = new byte[4096];
82         int bytes_read;
83
84         // check input file
85
File fin = new File(from);
86
87         if (!fin.exists()) {
88             throw new IllegalArgumentException JavaDoc(from + " does not exist.");
89         }
90
91         if (!fin.canRead()) {
92             throw new IllegalArgumentException JavaDoc(from + " read protected.");
93         }
94
95         ZipInputStream in = new ZipInputStream(new FileInputStream(from));
96         ZipEntry entry;
97         FileOutputStream out;
98
99         while ((entry = in.getNextEntry()) != null) {
100             String JavaDoc 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 }
Popular Tags