Java Reference
In-Depth Information
Example 10-12. UnZip.java
public
public class
UnZip
{
/** Constants for mode listing or mode extracting. */
public
class
UnZip
public static
static enum
enum
Mode
{
LIST
,
EXTRACT
;
};
/** Whether we are extracting or just printing TOC */
protected
protected
Mode mode
=
Mode
.
LIST
;
/** The ZipFile that is used to read an archive */
protected
protected
ZipFile zippy
;
/** The buffer for reading/writing the ZipFile data */
protected
protected
byte
byte
[]
b
=
new
new
byte
byte
[
8092
];;
/** Simple main program, construct an UnZipper, process each
* .ZIP file from argv[] through that object.
*/
public
public static
void
main
(
String
[]
argv
) {
UnZip u
=
new
static
void
new
UnZip
();
for
for
(
int
int
i
=
0
;
i
<
argv
.
length
;
i
++) {
iif
(
"-x"
.
equals
(
argv
[
i
])) {
u
.
setMode
(
Mode
.
EXTRACT
);
continue
continue
;
}
String candidate
=
argv
[
i
];
// System.err.println("Trying path " + candidate);
iif
(
candidate
.
endsWith
(
".zip"
) ||
candidate
.
endsWith
(
".jar"
))
u
.
unZip
(
candidate
);
else
else
System
.
err
.
println
(
"Not a zip file? "
+
candidate
);
}
System
.
err
.
println
(
"All done!"
);
}
/** Set the Mode (list, extract). */
protected
protected
void
void
setMode
(
Mode m
) {
mode
=
m
;
}
/** Cache of paths we've mkdir()ed. */
protected
protected
SortedSet
<
String
>
dirsMade
;