KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > util > ZipFileCreator


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.util;
17
18 import net.sf.jftp.*;
19 import net.sf.jftp.net.*;
20 import net.sf.jftp.system.logging.Log;
21 import net.sf.jftp.util.*;
22
23 import java.io.*;
24
25 import java.util.zip.*;
26   
27  
28 public class ZipFileCreator
29 {
30     private ZipOutputStream z;
31
32     public ZipFileCreator(String JavaDoc[] files, String JavaDoc path, String JavaDoc name)
33                    throws Exception JavaDoc
34     {
35         z = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(path +
36                                                                               name)));
37         perform(files, path, "");
38         z.finish();
39         z.flush();
40         z.close();
41     }
42
43     private void perform(String JavaDoc[] files, String JavaDoc path, String JavaDoc offset)
44     {
45         byte[] buf = new byte[4096];
46
47         for(int i = 0; i < files.length; i++)
48         {
49             try
50             {
51                 File f = new File(path + offset + files[i]);
52                 BufferedInputStream in = null;
53
54                 if(f.exists() && !f.isDirectory())
55                 {
56                     in = new BufferedInputStream(new FileInputStream(path +
57                                                                      offset +
58                                                                      files[i]));
59                 }
60                 else if(f.exists())
61                 {
62                     if(!files[i].endsWith("/"))
63                     {
64                         files[i] = files[i] + "/";
65                     }
66
67                     perform(f.list(), path, offset + files[i]);
68                 }
69
70                 ZipEntry tmp = new ZipEntry(offset + files[i]);
71                 z.putNextEntry(tmp);
72
73                 int len = 0;
74
75                 while((in != null) && (len != StreamTokenizer.TT_EOF))
76                 {
77                     len = in.read(buf);
78
79                     if(len == StreamTokenizer.TT_EOF)
80                     {
81                         break;
82                     }
83
84                     z.write(buf, 0, len);
85                 }
86
87                 z.closeEntry();
88             }
89             catch(Exception JavaDoc ex)
90             {
91                 Log.debug("Skipping a file (no permission?)");
92             }
93         }
94     }
95 }
96
Popular Tags