741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
55 |
|
1652 |
22 May 07 |
gregory |
56 |
package se.lu.thep.coreftpd.common; |
741 |
10 Oct 06 |
olle |
57 |
|
791 |
23 Oct 06 |
olle |
58 |
import se.lu.thep.coreftpd.webserver.ReadInputStream; |
741 |
10 Oct 06 |
olle |
59 |
import java.io.BufferedInputStream; |
741 |
10 Oct 06 |
olle |
60 |
import java.io.BufferedOutputStream; |
741 |
10 Oct 06 |
olle |
61 |
import java.io.File; |
741 |
10 Oct 06 |
olle |
62 |
import java.io.FileOutputStream; |
741 |
10 Oct 06 |
olle |
63 |
import java.util.Enumeration; |
741 |
10 Oct 06 |
olle |
64 |
import java.util.zip.ZipEntry; |
741 |
10 Oct 06 |
olle |
65 |
import java.util.zip.ZipFile; |
741 |
10 Oct 06 |
olle |
66 |
|
741 |
10 Oct 06 |
olle |
67 |
/** |
1652 |
22 May 07 |
gregory |
68 |
* |
1652 |
22 May 07 |
gregory |
* <B>How to use:</B> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>new ExtractZipFile(String zipFileName, boolean b_overwriteFiles);</CODE> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>new ExtractZipFile(File zipFile, boolean b_overwriteFiles);</CODE> |
741 |
10 Oct 06 |
olle |
* <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
75 |
* |
1652 |
22 May 07 |
gregory |
* <B>More info:</B> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>ExtractZipFile</CODE> opens a zip-file and extracts the files in the |
1652 |
22 May 07 |
gregory |
* zip-file. <BR> |
1652 |
22 May 07 |
gregory |
* This class does also support zip-files with directories (directories will be |
1652 |
22 May 07 |
gregory |
* created and files moved into the directories). <BR> |
1652 |
22 May 07 |
gregory |
* Set <CODE>b_overwriteFiles = true</CODE> and files will be overwritten if |
1652 |
22 May 07 |
gregory |
* they already exists. <BR> |
1652 |
22 May 07 |
gregory |
* Set <CODE>b_overwriteFiles = false</CODE> and files will NOT be overwritten |
1652 |
22 May 07 |
gregory |
* if they already exists. |
1652 |
22 May 07 |
gregory |
85 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
88 |
*/ |
741 |
10 Oct 06 |
olle |
89 |
|
1652 |
22 May 07 |
gregory |
90 |
public class ExtractZipFile { |
1652 |
22 May 07 |
gregory |
91 |
protected final boolean b_showErrors = false; |
741 |
10 Oct 06 |
olle |
92 |
|
1652 |
22 May 07 |
gregory |
93 |
protected boolean b_overwriteFiles = false; // this will be set in the |
1652 |
22 May 07 |
gregory |
// constructor as well... |
1652 |
22 May 07 |
gregory |
95 |
|
1652 |
22 May 07 |
gregory |
96 |
public ExtractZipFile(String s, boolean argOverwriteFiles) { |
741 |
10 Oct 06 |
olle |
97 |
this(new File(s), argOverwriteFiles); |
741 |
10 Oct 06 |
olle |
98 |
} |
741 |
10 Oct 06 |
olle |
99 |
|
1652 |
22 May 07 |
gregory |
100 |
public ExtractZipFile(File zipFile, boolean argOverwriteFiles) { |
741 |
10 Oct 06 |
olle |
101 |
this(zipFile, argOverwriteFiles, null); |
741 |
10 Oct 06 |
olle |
102 |
} |
741 |
10 Oct 06 |
olle |
103 |
|
1652 |
22 May 07 |
gregory |
104 |
public ExtractZipFile(String s, boolean argOverwriteFiles, |
1652 |
22 May 07 |
gregory |
105 |
String s_defaultPath) { |
741 |
10 Oct 06 |
olle |
106 |
this(new File(s), argOverwriteFiles, s_defaultPath); |
741 |
10 Oct 06 |
olle |
107 |
} |
741 |
10 Oct 06 |
olle |
108 |
|
1652 |
22 May 07 |
gregory |
109 |
public ExtractZipFile(File zipFile, boolean argOverwriteFiles, |
1652 |
22 May 07 |
gregory |
110 |
String s_defaultPath) { |
741 |
10 Oct 06 |
olle |
111 |
try { |
1652 |
22 May 07 |
gregory |
112 |
if (s_defaultPath == null) |
1652 |
22 May 07 |
gregory |
113 |
s_defaultPath = ""; |
1652 |
22 May 07 |
gregory |
114 |
else if ((new File(s_defaultPath)).exists()) |
1652 |
22 May 07 |
gregory |
115 |
s_defaultPath = (new File(s_defaultPath)).getPath() |
1652 |
22 May 07 |
gregory |
116 |
+ File.separator; |
1652 |
22 May 07 |
gregory |
117 |
else |
1652 |
22 May 07 |
gregory |
118 |
s_defaultPath = ""; |
741 |
10 Oct 06 |
olle |
119 |
|
1652 |
22 May 07 |
gregory |
120 |
b_overwriteFiles = argOverwriteFiles; |
1652 |
22 May 07 |
gregory |
121 |
ZipFile ZF_theZipFile = new ZipFile(zipFile); |
1652 |
22 May 07 |
gregory |
// Enumeration e_allFiles=ZF_theZipFile.entries(); |
741 |
10 Oct 06 |
olle |
123 |
|
1652 |
22 May 07 |
gregory |
// System.out.println("Folders:"); |
1652 |
22 May 07 |
gregory |
// First, create all folders |
1652 |
22 May 07 |
gregory |
126 |
for (Enumeration e = ZF_theZipFile.entries(); e.hasMoreElements();) { |
1652 |
22 May 07 |
gregory |
127 |
ZipEntry myZipEntry = (ZipEntry) e.nextElement(); |
1652 |
22 May 07 |
gregory |
128 |
if (myZipEntry.isDirectory()) { |
1652 |
22 May 07 |
gregory |
129 |
File newDir = new File(s_defaultPath + myZipEntry.getName()); |
1652 |
22 May 07 |
gregory |
130 |
newDir.mkdirs(); |
1652 |
22 May 07 |
gregory |
131 |
} |
741 |
10 Oct 06 |
olle |
132 |
} |
741 |
10 Oct 06 |
olle |
133 |
|
1652 |
22 May 07 |
gregory |
// System.out.println("Files:"); |
1652 |
22 May 07 |
gregory |
// Then extract all the files |
1652 |
22 May 07 |
gregory |
136 |
for (Enumeration e = ZF_theZipFile.entries(); e.hasMoreElements();) { |
1652 |
22 May 07 |
gregory |
137 |
ZipEntry myZipEntry = (ZipEntry) e.nextElement(); |
1652 |
22 May 07 |
gregory |
138 |
if (!myZipEntry.isDirectory() |
1652 |
22 May 07 |
gregory |
139 |
&& (overWriteFile(myZipEntry) || !(new File(myZipEntry |
1652 |
22 May 07 |
gregory |
140 |
.getName())).exists())) // if [this is not a |
1652 |
22 May 07 |
gregory |
// directory] and [[we |
1652 |
22 May 07 |
gregory |
// shall overwrite] or |
1652 |
22 May 07 |
gregory |
// [this file doesn't |
1652 |
22 May 07 |
gregory |
// exists]]... |
1652 |
22 May 07 |
gregory |
145 |
{ |
1652 |
22 May 07 |
gregory |
146 |
BufferedInputStream bis = new BufferedInputStream( |
1652 |
22 May 07 |
gregory |
147 |
ZF_theZipFile.getInputStream(myZipEntry)); |
1652 |
22 May 07 |
gregory |
148 |
BufferedOutputStream bos; |
1652 |
22 May 07 |
gregory |
149 |
bos = new BufferedOutputStream(new FileOutputStream( |
1652 |
22 May 07 |
gregory |
150 |
s_defaultPath + myZipEntry.getName())); |
741 |
10 Oct 06 |
olle |
151 |
|
1652 |
22 May 07 |
gregory |
152 |
(new ReadInputStream(bis, bos)).start(); |
1652 |
22 May 07 |
gregory |
153 |
} |
741 |
10 Oct 06 |
olle |
154 |
} |
1652 |
22 May 07 |
gregory |
155 |
} catch (Exception e) { |
1652 |
22 May 07 |
gregory |
156 |
if (b_showErrors) |
1652 |
22 May 07 |
gregory |
157 |
System.out.println("An error has occured:\nProbably, \"" |
1652 |
22 May 07 |
gregory |
158 |
+ zipFile.getPath() |
1652 |
22 May 07 |
gregory |
159 |
+ "\" was not found.\nThe error output is: " + e); |
741 |
10 Oct 06 |
olle |
160 |
} |
741 |
10 Oct 06 |
olle |
161 |
} |
741 |
10 Oct 06 |
olle |
162 |
|
1652 |
22 May 07 |
gregory |
163 |
boolean overWriteFile(ZipEntry myZipEntry) { |
741 |
10 Oct 06 |
olle |
164 |
return b_overwriteFiles; |
741 |
10 Oct 06 |
olle |
165 |
} |
741 |
10 Oct 06 |
olle |
166 |
} |