KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > ZipTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.common;
20
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27 import java.util.zip.ZipInputStream JavaDoc;
28
29 /**
30  * This class supplies some methods of usefullness for the
31  * management of zip file.
32  * <p>In particular it supplies methods for the check and for
33  * the extraction of zip file
34  *
35  * @version $Id: ZipTools.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
36  * @author Stefano Nichele
37  */

38
39 public class ZipTools {
40     
41     
42     // -----------------------------------------------------------Public methods
43
/**
44      * Extract the zip file rappresented by the given byte array.
45      * @param workingDirectory the directory in which the file
46      * should be extracted
47      * @param zipFile the byte array that represents the zip
48      * @throws Exception if error occurs or if the zip file is not valid
49      */

50     public static void extract(String JavaDoc workingDirectory, byte[] zipFile)
51     throws Exception JavaDoc {
52         
53         ByteArrayInputStream JavaDoc byteStream = new ByteArrayInputStream JavaDoc(zipFile);
54         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
55         
56         ZipInputStream JavaDoc zipStream = new ZipInputStream JavaDoc(byteStream);
57         ZipEntry JavaDoc zipEntry = null;
58         
59         String JavaDoc nameZipEntry = null;
60         
61         byte[] contentZipEntry = null;
62         boolean isDirectory = false;
63         
64         int indexFileSeparator = -1;
65         String JavaDoc directory = null;
66         String JavaDoc fileName = null;
67         
68         while ( (zipEntry = zipStream.getNextEntry()) != null ) {
69             nameZipEntry = workingDirectory + File.separator + zipEntry.getName();
70             
71             isDirectory = zipEntry.isDirectory();
72             
73             if (isDirectory) {
74                 File JavaDoc file = new File JavaDoc(nameZipEntry);
75                 file.mkdirs();
76             } else {
77                 
78                 // read zipEntry
79
byte[] buf = new byte[1024];
80                 int c = 0;
81                 
82                 while ( (c = zipStream.read(buf)) != -1) {
83                     out.write(buf, 0, c);
84                 }
85                 
86                 indexFileSeparator = nameZipEntry.lastIndexOf(File.separator);
87                 directory = nameZipEntry.substring(0, indexFileSeparator);
88                 fileName = nameZipEntry.substring(indexFileSeparator+1,nameZipEntry.length());
89                 
90                 FileSystemTools.createFile(directory, fileName, out);
91                 
92                 out.reset();
93                 zipStream.closeEntry();
94             }
95         }
96         zipStream.close();
97         byteStream.close();
98     }
99     
100     /**
101      * Verify if the given byte array represents a valid zip file.
102      * Reads all the content of the zip file but it does not save it
103      * @param zipFile the byte array to verify
104      * @throws IOException if the byte array does not represent a valid zip file
105      */

106     public static void verifyZip(byte[] zipFile) throws IOException JavaDoc {
107         
108         boolean isValidZipFile = true;
109         
110         ByteArrayInputStream JavaDoc bStream = new ByteArrayInputStream JavaDoc(zipFile);
111         
112         ZipInputStream JavaDoc zipStream = new ZipInputStream JavaDoc(bStream);
113         
114         ZipEntry JavaDoc zipEntry = null;
115         
116         String JavaDoc nameZipEntry = null;
117         int dimZipEntry = 0;
118         byte[] contentZipEntry = null;
119         boolean isDirectory = false;
120         
121         while ( (zipEntry = zipStream.getNextEntry()) != null ) {
122             
123             isDirectory = zipEntry.isDirectory();
124             
125             if (!isDirectory) {
126                 // read zipEntry
127
int c = -1;
128                 byte[] buf = new byte[1024];
129                 while ( (c = zipStream.read(buf)) != -1) {
130                     dimZipEntry = dimZipEntry + c;
131                 }
132                 dimZipEntry = 0;
133                 zipStream.closeEntry();
134             }
135         }
136         zipStream.close();
137         bStream.close();
138     }
139 }
Popular Tags