Java Reference
In-Depth Information
/** For a given Zip file, process each entry. */
public
public
void
void
unZip
(
String fileName
) {
dirsMade
=
new
new
TreeSet
<
String
>();
try
try
{
zippy
=
new
new
ZipFile
(
fileName
);
Enumeration all
=
zippy
.
entries
();
while
while
(
all
.
hasMoreElements
()) {
getFile
((
ZipEntry
)
all
.
nextElement
());
}
}
catch
catch
(
IOException err
) {
System
.
err
.
println
(
"IO Error: "
+
err
);
return
return
;
}
}
protected
protected
boolean
boolean
warnedMkDir
=
false
false
;
/** Process one file from the zip, given its name.
* Either print the name, or create the file on disk.
*/
protected
protected
void
void
getFile
(
ZipEntry e
)
throws
throws
IOException
{
String zipName
=
e
.
getName
();
switch
switch
(
mode
) {
case
case
EXTRACT:
iif
(
zipName
.
startsWith
(
"/"
)) {
iif
(!
warnedMkDir
)
System
.
out
.
println
(
"Ignoring absolute paths"
);
warnedMkDir
=
true
true
;
zipName
=
zipName
.
substring
(
1
);
}
// if a directory, just return. We mkdir for every file,
// since some widely used Zip creators don't put out
// any directory entries, or put them in the wrong place.
iif
(
zipName
.
endsWith
(
"/"
)) {
return
return
;
}
// Else must be a file; open the file for output
// Get the directory part.
int
int
ix
=
zipName
.
lastIndexOf
(
'/'
);
iif
(
ix
>
0
) {
String dirName
=
zipName
.
substring
(
0
,
ix
);
iif
(!
dirsMade
.
contains
(
dirName
)) {
File d
=
new
new
File
(
dirName
);
// If it already exists as a dir, don't do anything
iif
(!(
d
.
exists
() &&
d
.
isDirectory
())) {
// Try to create the directory, warn if it fails