KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > zip > Zipper


1 package de.jwi.zip;
2
3 /*
4  * jFM - Java Web File Manager
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jFM.
9  *
10  * jFM is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jFM is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jFM; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipOutputStream JavaDoc;
33
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.filefilter.IOFileFilter;
36
37 /**
38  * @author Jürgen Weber
39  * Source file created on 30.03.2004
40  *
41  */

42 public class Zipper
43 {
44
45     private static final int BUFSIZE = 1024;
46
47     /*
48      * @param base if base != null && f.getPath().startsWith(base.getPath())
49      * then skip base.getPath() from zip entry name
50      */

51
52     public void zip(ZipOutputStream JavaDoc out, File JavaDoc f, File JavaDoc base)
53             throws IOException JavaDoc
54     {
55         String JavaDoc name = f.getPath().replace('\\', '/');
56
57         if (base != null)
58         {
59             String JavaDoc basename = base.getPath().replace('\\', '/');
60             
61             if (name.startsWith(basename))
62             {
63                 name = name.substring(basename.length());
64             }
65         }
66
67         if (name.startsWith("/"))
68         {
69             name = name.substring(1);
70         }
71
72         ZipEntry JavaDoc entry = new ZipEntry JavaDoc(name);
73         entry.setTime(f.lastModified());
74
75         out.putNextEntry(entry);
76
77         FileInputStream JavaDoc is = new FileInputStream JavaDoc(f);
78
79         byte[] buf = new byte[BUFSIZE];
80
81         try
82         {
83         int l;
84         while ((l = is.read(buf)) > -1)
85         {
86             out.write(buf, 0, l);
87         }
88         }
89         finally
90         {
91             is.close();
92         }
93         out.closeEntry();
94
95     }
96
97     public void zip(ZipOutputStream JavaDoc out, Collection JavaDoc c, File JavaDoc vpath)
98             throws IOException JavaDoc
99     {
100         Iterator JavaDoc it = c.iterator();
101         while (it.hasNext())
102         {
103             File JavaDoc f = (File JavaDoc) it.next();
104             zip(out, f, vpath);
105         }
106     }
107
108     public static void main(String JavaDoc[] args) throws Exception JavaDoc
109     {
110         ZipOutputStream JavaDoc z = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(
111                 "d:/temp/myfirst.zip"));
112
113         IOFileFilter filter = new IOFileFilter()
114         {
115
116             public boolean accept(java.io.File JavaDoc file)
117             {
118                 return true;
119             }
120
121             public boolean accept(java.io.File JavaDoc dir, java.lang.String JavaDoc name)
122             {
123                 return true;
124             }
125         };
126
127         Collection JavaDoc c = FileUtils.listFiles(new File JavaDoc(
128                 "/java/javadocs/j2sdk-1.4.1/docs/tooldocs"), filter, filter);
129
130         new Zipper().zip(z, c, new File JavaDoc("/java/javadocs/j2sdk-1.4.1"));
131
132         z.close();
133
134     }
135 }
Popular Tags