KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > Zipper


1 package com.bull.eclipse.jonas.utils;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.BufferedOutputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Vector JavaDoc;
15 import java.util.zip.CRC32 JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18 import java.util.zip.ZipOutputStream JavaDoc;
19
20 import com.bull.eclipse.jonas.ExtraResourcesEntries;
21
22
23 public class Zipper {
24
25     private static final int BUFFER = 2048;
26
27     private static final long EMPTY_CRC = new CRC32 JavaDoc ().getValue ();
28     protected String JavaDoc emptyBehavior = "skip";
29     private Vector JavaDoc filesets = new Vector JavaDoc ();
30     
31     protected Hashtable JavaDoc addedDirs = new Hashtable JavaDoc();
32     private File JavaDoc outputFile = null;
33     private File JavaDoc directory = null;
34     private ExtraResourcesEntries extraList = null;
35     private FileOutputStream JavaDoc fos = null;
36     private ZipOutputStream JavaDoc zos = null;
37     private String JavaDoc currentDirName;
38
39     public Zipper(File JavaDoc outputFile, File JavaDoc directory) throws IOException JavaDoc {
40         this.outputFile = outputFile;
41         this.directory = directory;
42         currentDirName = directory.getAbsolutePath();
43     }
44     
45     public Zipper(File JavaDoc outputFile, File JavaDoc directory, ExtraResourcesEntries extraList) throws IOException JavaDoc {
46         this.outputFile = outputFile;
47         this.directory = directory;
48         this.extraList = extraList;
49         currentDirName = directory.getAbsolutePath();
50     }
51     
52     public void zip() throws IOException JavaDoc {
53         fos = new FileOutputStream JavaDoc(outputFile);
54         zos = new ZipOutputStream JavaDoc(fos);
55         zipDir(directory);
56         zipExtraResources(extraList);
57         ArrayList JavaDoc a ;
58         zos.flush();
59         zos.close();
60         fos.close();
61     }
62
63
64     static public void unzip(ZipEntry JavaDoc zipEntry,ZipFile JavaDoc zipFile,File JavaDoc output) throws IOException JavaDoc {
65         copyInputStream(zipFile.getInputStream(zipEntry), new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(output)));
66     }
67     
68     
69     private void zipDir(File JavaDoc dir) throws IOException JavaDoc {
70         if(!dir.getPath().equals(currentDirName)) {
71             String JavaDoc entryName = dir.getPath().substring(currentDirName.length()+1);
72             entryName = entryName.replace('\\', '/');
73             ZipEntry JavaDoc ze = new ZipEntry JavaDoc (entryName + "/");
74             if (dir != null && dir.exists()) {
75                 ze.setTime(dir.lastModified());
76             } else {
77                 ze.setTime(System.currentTimeMillis());
78             }
79             ze.setSize (0);
80             ze.setMethod (ZipEntry.STORED);
81             // This is faintly ridiculous:
82
ze.setCrc (EMPTY_CRC);
83             zos.putNextEntry (ze);
84         }
85                         
86         if (dir.exists() && dir.isDirectory()) {
87             File JavaDoc [] fileList = dir.listFiles();
88
89             for (int i = 0; i < fileList.length; i++) {
90                 if (fileList[i].isDirectory() && this.acceptDir(fileList[i])) {
91                     zipDir(fileList[i]);
92                 }
93                 if (fileList[i].isFile() && this.acceptFile(fileList[i])) {
94                     zipFile(fileList[i]);
95                 }
96             }
97         }
98
99     }
100
101
102     private void zipExtraResources(ExtraResourcesEntries extraList) throws IOException JavaDoc {
103         if (extraList != null) {
104             List JavaDoc resourcesList = extraList.getList();
105             int i = 0;
106             String JavaDoc tempFile = null;
107             while (i < resourcesList.size()) {
108                 tempFile = new File JavaDoc ((String JavaDoc)resourcesList.get(i)).getName();
109                 zipFile(new File JavaDoc((String JavaDoc)resourcesList.get(i)) , "WEB-INF" + File.separator + "lib" + File.separator + tempFile);
110                 i++;
111             }
112         }
113     }
114
115     private void zipFile(File JavaDoc file) throws IOException JavaDoc {
116         if(!file.equals(this.outputFile)) {
117             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file),BUFFER);
118     
119             String JavaDoc entryName = file.getPath().substring(currentDirName.length()+1);
120             entryName = entryName.replace('\\', '/');
121             ZipEntry JavaDoc fileEntry = new ZipEntry JavaDoc(entryName);
122             zos.putNextEntry(fileEntry);
123     
124             byte[] data = new byte[BUFFER];
125             int byteCount;
126             while ((byteCount = bis.read(data, 0, BUFFER)) != -1) {
127                 zos.write(data, 0, byteCount);
128             }
129     
130             bis.close();
131         }
132     }
133     
134     private void zipFile(File JavaDoc file, String JavaDoc zipEntry) throws IOException JavaDoc {
135         if(!file.equals(this.outputFile)) {
136             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file),BUFFER);
137     
138             //String entryName = file.getPath().substring(currentDirName.length()+1);
139
String JavaDoc entryName = zipEntry;
140             entryName = entryName.replace('\\', '/');
141             ZipEntry JavaDoc fileEntry = new ZipEntry JavaDoc(entryName);
142             zos.putNextEntry(fileEntry);
143     
144             byte[] data = new byte[BUFFER];
145             int byteCount;
146             while ((byteCount = bis.read(data, 0, BUFFER)) != -1) {
147                 zos.write(data, 0, byteCount);
148             }
149     
150             bis.close();
151         }
152     }
153     
154     protected boolean acceptDir(File JavaDoc dir) {
155         return true;
156     }
157     
158     protected boolean acceptFile(File JavaDoc file) {
159         return true;
160     }
161     
162     
163     private static final void copyInputStream(InputStream JavaDoc in, OutputStream JavaDoc out)
164       throws IOException JavaDoc
165     {
166         byte[] buffer = new byte[1024];
167         int len;
168
169         while((len = in.read(buffer)) >= 0)
170           out.write(buffer, 0, len);
171
172         in.close();
173         out.close();
174     }
175
176 }
177
Popular Tags