KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbiplugin > util > ZipUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ZipUtil.java 410 2006-05-17 13:25:42Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.jbiplugin.util;
23
24 import java.io.DataInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipOutputStream JavaDoc;
30
31 /**
32  * Contains utilities methods in relation to Zip operations.
33  *
34  * @version $Rev: 410 $ $Date: 2006-05-17 15:25:42 +0200 (mer, 17 mai 2006) $
35  * @since Petals 1.0
36  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
37  * @author ddesjardins - eBMWebsourcing
38  */

39 public final class ZipUtil {
40
41     /**
42      * Do not construct.
43      */

44     private ZipUtil() {
45         super();
46     }
47
48     /**
49      * Add all the content of directory to a zip file to a specific entry
50      *
51      * @param archive
52      * zip file
53      * @param directory
54      * directory to add
55      * @param entryDirectory
56      * entry directory
57      * @throws Exception
58      * @throws PetalsException
59      */

60     public static void zipDirectory(File JavaDoc archive, File JavaDoc directory,
61             String JavaDoc entryDirectoryName) throws Exception JavaDoc {
62         if (directory.exists()) {
63             ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(
64                     new FileOutputStream JavaDoc(archive, true));
65             recurseDirectory(zipOutputStream, directory, entryDirectoryName);
66             zipOutputStream.flush();
67             zipOutputStream.close();
68         }
69     }
70
71     /**
72      * Recurse a directory to add its content to a zip file
73      *
74      * @param zipOutputStream
75      * zip file
76      * @param directory
77      * directory to recurse
78      * @param entryDirectoryName
79      * directory to put the content in the zip
80      * @throws Exception
81      */

82     private static void recurseDirectory(ZipOutputStream JavaDoc zipOutputStream,
83             File JavaDoc directory, String JavaDoc entryDirectoryName) throws Exception JavaDoc {
84         for (File JavaDoc file : directory.listFiles()) {
85             if (file.isFile()) {
86                 ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(entryDirectoryName
87                         + File.separator + file.getName());
88                 DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new FileInputStream JavaDoc(
89                         file));
90                 byte[] content = new byte[dis.available()];
91                 dis.readFully(content);
92                 zipOutputStream.putNextEntry(zipEntry);
93                 zipOutputStream.write(content);
94                 zipOutputStream.closeEntry();
95             } else {
96                 if (!file.getName().startsWith(".")) {
97                     // Avoid to add the .svn directory
98
recurseDirectory(zipOutputStream, file, entryDirectoryName
99                             + File.separator + file.getName());
100                 }
101             }
102         }
103     }
104 }
105
Popular Tags