01: /*
02: * This program is free software; you can redistribute it and/or
03: * modify it under the terms of the GNU General Public License
04: * as published by the Free Software Foundation; either version 2
05: * of the License, or (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * 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: public class ZipFileCreator {
28: private ZipOutputStream z;
29:
30: public ZipFileCreator(String[] files, String path, String name)
31: throws Exception {
32: z = new ZipOutputStream(new BufferedOutputStream(
33: new FileOutputStream(path + name)));
34: perform(files, path, "");
35: z.finish();
36: z.flush();
37: z.close();
38: }
39:
40: private void perform(String[] files, String path, String offset) {
41: byte[] buf = new byte[4096];
42:
43: for (int i = 0; i < files.length; i++) {
44: try {
45: File f = new File(path + offset + files[i]);
46: BufferedInputStream in = null;
47:
48: if (f.exists() && !f.isDirectory()) {
49: in = new BufferedInputStream(new FileInputStream(
50: path + offset + files[i]));
51: } else if (f.exists()) {
52: if (!files[i].endsWith("/")) {
53: files[i] = files[i] + "/";
54: }
55:
56: perform(f.list(), path, offset + files[i]);
57: }
58:
59: ZipEntry tmp = new ZipEntry(offset + files[i]);
60: z.putNextEntry(tmp);
61:
62: int len = 0;
63:
64: while ((in != null) && (len != StreamTokenizer.TT_EOF)) {
65: len = in.read(buf);
66:
67: if (len == StreamTokenizer.TT_EOF) {
68: break;
69: }
70:
71: z.write(buf, 0, len);
72: }
73:
74: z.closeEntry();
75: } catch (Exception ex) {
76: Log.debug("Skipping a file (no permission?)");
77: }
78: }
79: }
80: }
|