|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.OutputStream
org.apache.commons.compress.archivers.ArchiveOutputStream
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
public class ZipArchiveOutputStream
Reimplementation of java.util.zip.ZipOutputStream that does handle the extended
functionality of this package, especially internal/external file
attributes and extra fields with different layouts for local file
data and central directory entries.
This class will try to use RandomAccessFile when you know that the output is going to go to a
file.
If RandomAccessFile cannot be used, this implementation will use
a Data Descriptor to store size and CRC information for DEFLATED entries, this means, you don't need to
calculate them yourself. Unfortunately this is not possible for
the STORED method, here setting the CRC and
uncompressed size information is required before putArchiveEntry(ArchiveEntry) can be called.
| Nested Class Summary | |
|---|---|
static class |
ZipArchiveOutputStream.UnicodeExtraFieldPolicy
enum that represents the possible policies for creating Unicode extra fields. |
| Field Summary | |
|---|---|
protected Deflater |
def
This Deflater object is used for output. |
static int |
DEFAULT_COMPRESSION
Default compression level for deflated entries. |
static int |
DEFLATED
Compression method for deflated entries. |
static int |
EFS_FLAG
Deprecated. use GeneralPurposeBit.UFT8_NAMES_FLAG instead |
protected boolean |
finished
indicates if this archive is finished. protected for use in Jar implementation |
static int |
STORED
Compression method for stored entries. |
| Constructor Summary | |
|---|---|
ZipArchiveOutputStream(File file)
Creates a new ZIP OutputStream writing to a File. |
|
ZipArchiveOutputStream(OutputStream out)
Creates a new ZIP OutputStream filtering the underlying stream. |
|
| Method Summary | |
|---|---|
boolean |
canWriteEntryData(ArchiveEntry ae)
Whether this stream is able to write the given entry. |
void |
close()
Closes this output stream and releases any system resources associated with the stream. |
void |
closeArchiveEntry()
Writes all necessary data for this entry. |
ArchiveEntry |
createArchiveEntry(File inputFile,
String entryName)
Creates a new zip entry taking some information from the given file and using the provided name. |
protected void |
deflate()
Writes next block of compressed data to the output stream. |
void |
finish()
Finishes the addition of entries to this stream, without closing it. |
void |
flush()
Flushes this output stream and forces any buffered output bytes to be written out to the stream. |
String |
getEncoding()
The encoding to use for filenames and the file comment. |
boolean |
isSeekable()
This method indicates whether this archive is writing to a seekable stream (i.e., to a random access file). |
void |
putArchiveEntry(ArchiveEntry archiveEntry)
Writes the headers for an archive entry to the output stream. |
void |
setComment(String comment)
Set the file comment. |
void |
setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b)
Whether to create Unicode Extra Fields. |
void |
setEncoding(String encoding)
The encoding to use for filenames and the file comment. |
void |
setFallbackToUTF8(boolean b)
Whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding. |
void |
setLevel(int level)
Sets the compression level for subsequent entries. |
void |
setMethod(int method)
Sets the default compression method for subsequent entries. |
void |
setUseLanguageEncodingFlag(boolean b)
Whether to set the language encoding flag if the file name encoding is UTF-8. |
void |
write(byte[] b,
int offset,
int length)
Writes bytes to ZIP entry. |
protected void |
writeCentralDirectoryEnd()
Writes the "End of central dir record". |
protected void |
writeCentralFileHeader(ZipArchiveEntry ze)
Writes the central file header entry. |
protected void |
writeDataDescriptor(ZipArchiveEntry ze)
Writes the data descriptor entry. |
protected void |
writeLocalFileHeader(ZipArchiveEntry ze)
Writes the local file header entry |
protected void |
writeOut(byte[] data)
Write bytes to output or random access file. |
protected void |
writeOut(byte[] data,
int offset,
int length)
Write bytes to output or random access file. |
| Methods inherited from class org.apache.commons.compress.archivers.ArchiveOutputStream |
|---|
count, count, getBytesWritten, getCount, write |
| Methods inherited from class java.io.OutputStream |
|---|
write |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected boolean finished
public static final int DEFLATED
public static final int DEFAULT_COMPRESSION
public static final int STORED
public static final int EFS_FLAG
GeneralPurposeBit.UFT8_NAMES_FLAG instead
protected final Deflater def
| Constructor Detail |
|---|
public ZipArchiveOutputStream(OutputStream out)
out - the outputstream to zip
public ZipArchiveOutputStream(File file)
throws IOException
file - the file to zip to
IOException - on error| Method Detail |
|---|
public boolean isSeekable()
For seekable streams, you don't need to calculate the CRC or
uncompressed size for STORED entries before
invoking putArchiveEntry(ArchiveEntry).
public void setEncoding(String encoding)
For a list of possible values see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html. Defaults to UTF-8.
encoding - the encoding to use for file names, use null
for the platform's default encodingpublic String getEncoding()
public void setUseLanguageEncodingFlag(boolean b)
Defaults to true.
public void setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b)
Defaults to NEVER.
public void setFallbackToUTF8(boolean b)
Defaults to false.
public void finish()
throws IOException
finish in class ArchiveOutputStreamIOException
public void closeArchiveEntry()
throws IOException
closeArchiveEntry in class ArchiveOutputStreamIOException - on error
public void putArchiveEntry(ArchiveEntry archiveEntry)
throws IOException
ArchiveOutputStream.closeArchiveEntry() to complete the process.
putArchiveEntry in class ArchiveOutputStreamarchiveEntry - describes the entry
ClassCastException - if entry is not an instance of ZipArchiveEntry
IOExceptionpublic void setComment(String comment)
comment - the commentpublic void setLevel(int level)
Default is Deflater.DEFAULT_COMPRESSION.
level - the compression level.
IllegalArgumentException - if an invalid compression
level is specified.public void setMethod(int method)
Default is DEFLATED.
method - an int from java.util.zip.ZipEntrypublic boolean canWriteEntryData(ArchiveEntry ae)
May return false if it is set up to use encryption or a compression method that hasn't been implemented yet.
canWriteEntryData in class ArchiveOutputStream
public void write(byte[] b,
int offset,
int length)
throws IOException
write in class OutputStreamb - the byte array to writeoffset - the start position to write fromlength - the number of bytes to write
IOException - on error
public void close()
throws IOException
close in interface Closeableclose in class OutputStreamIOException - if an I/O error occurs.
public void flush()
throws IOException
flush in interface Flushableflush in class OutputStreamIOException - if an I/O error occurs.
protected final void deflate()
throws IOException
IOException - on error
protected void writeLocalFileHeader(ZipArchiveEntry ze)
throws IOException
ze - the entry to write
IOException - on error
protected void writeDataDescriptor(ZipArchiveEntry ze)
throws IOException
ze - the entry to write
IOException - on error
protected void writeCentralFileHeader(ZipArchiveEntry ze)
throws IOException
ze - the entry to write
IOException - on error
protected void writeCentralDirectoryEnd()
throws IOException
IOException - on error
protected final void writeOut(byte[] data)
throws IOException
data - the byte array to write
IOException - on error
protected final void writeOut(byte[] data,
int offset,
int length)
throws IOException
data - the byte array to writeoffset - the start position to write fromlength - the number of bytes to write
IOException - on error
public ArchiveEntry createArchiveEntry(File inputFile,
String entryName)
throws IOException
The name will be adjusted to end with a forward slash "/" if the file is a directory. If the file is not a directory a potential trailing forward slash will be stripped from the entry name.
Must not be used if the stream has already been closed.
createArchiveEntry in class ArchiveOutputStreamIOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||