This class adds support for file name encodings other than UTF-8
(which is required to work on ZIP files created by native zip tools
and is able to skip a preamble like the one found in self
extracting archives. Furthermore it returns instances of
org.apache.tools.zip.ZipEntry instead of
java.util.zip.ZipEntry.
| Constructor: |
public ZipFile(File f) throws IOException {
this(f, null);
}
Opens the given file for reading, assuming the platform's
native encoding for file names. Parameters:
f - the archive.
Throws:
IOException - if an error occurs while reading the file.
|
public ZipFile(String name) throws IOException {
this(new File(name), null);
}
Opens the given file for reading, assuming the platform's
native encoding for file names. Parameters:
name - name of the archive.
Throws:
IOException - if an error occurs while reading the file.
|
public ZipFile(String name,
String encoding) throws IOException {
this(new File(name), encoding, true);
}
Opens the given file for reading, assuming the specified
encoding for file names, scanning unicode extra fields. Parameters:
name - name of the archive.
encoding - the encoding to use for file names
Throws:
IOException - if an error occurs while reading the file.
|
public ZipFile(File f,
String encoding) throws IOException {
this(f, encoding, true);
}
Opens the given file for reading, assuming the specified
encoding for file names and scanning for unicode extra fields. Parameters:
f - the archive.
encoding - the encoding to use for file names, use null
for the platform's default encoding
Throws:
IOException - if an error occurs while reading the file.
|
public ZipFile(File f,
String encoding,
boolean useUnicodeExtraFields) throws IOException {
this.encoding = encoding;
this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
this.useUnicodeExtraFields = useUnicodeExtraFields;
archive = new RandomAccessFile(f, "r");
boolean success = false;
try {
Map entriesWithoutUTF8Flag = populateFromCentralDirectory();
resolveLocalFileHeaderData(entriesWithoutUTF8Flag);
success = true;
} finally {
if (!success) {
try {
archive.close();
} catch (IOException e2) {
// swallow, throw the original exception instead
}
}
}
}
Opens the given file for reading, assuming the specified
encoding for file names. Parameters:
f - the archive.
encoding - the encoding to use for file names, use null
for the platform's default encoding
useUnicodeExtraFields - whether to use InfoZIP Unicode
Extra Fields (if present) to set the file names.
Throws:
IOException - if an error occurs while reading the file.
|
| Method from org.apache.tools.zip.ZipFile Detail: |
public void close() throws IOException {
archive.close();
}
|
public static void closeQuietly(ZipFile zipfile) {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
//ignore
}
}
}
close a zipfile quietly; throw no io fault, do nothing
on a null parameter |
protected static Date fromDosTime(ZipLong zipDosTime) {
long dosTime = zipDosTime.getValue();
return new Date(dosToJavaTime(dosTime));
}
Convert a DOS date/time field to a Date object. |
public String getEncoding() {
return encoding;
}
The encoding to use for filenames and the file comment. |
public Enumeration getEntries() {
return Collections.enumeration(entries.keySet());
}
|
public ZipEntry getEntry(String name) {
return (ZipEntry) nameMap.get(name);
}
Returns a named entry - or null if no entry by
that name exists. |
public InputStream getInputStream(ZipEntry ze) throws IOException, ZipException {
OffsetEntry offsetEntry = (OffsetEntry) entries.get(ze);
if (offsetEntry == null) {
return null;
}
long start = offsetEntry.dataOffset;
BoundedInputStream bis =
new BoundedInputStream(start, ze.getCompressedSize());
switch (ze.getMethod()) {
case ZipEntry.STORED:
return bis;
case ZipEntry.DEFLATED:
bis.addDummy();
return new InflaterInputStream(bis, new Inflater(true));
default:
throw new ZipException("Found unsupported compression method "
+ ze.getMethod());
}
}
Returns an InputStream for reading the contents of the given entry. |
protected String getString(byte[] bytes) throws ZipException {
try {
return ZipEncodingHelper.getZipEncoding(encoding).decode(bytes);
} catch (IOException ex) {
throw new ZipException("Failed to decode name: " + ex.getMessage());
}
}
Retrieve a String from the given bytes using the encoding set
for this ZipFile. |