org.apache.commons.compress.archivers.zip
public class ZipArchiveOutputStream extends ArchiveOutputStream
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.
Modifier and Type | Class and Description |
---|---|
static class |
ZipArchiveOutputStream.UnicodeExtraFieldPolicy
enum that represents the possible policies for creating Unicode
extra fields.
|
Modifier and Type | Field and Description |
---|---|
private byte[] |
buf
This buffer servers as a Deflater.
|
(package private) static int |
BUFFER_SIZE |
(package private) static int |
BYTE_MASK |
private long |
cdLength
Length of central directory.
|
private long |
cdOffset
Start of central directory.
|
(package private) static byte[] |
CFH_SIG
central file header signature
|
private java.lang.String |
comment
The file comment.
|
private java.util.zip.CRC32 |
crc
CRC instance to avoid parsing DEFLATED data twice.
|
private ZipArchiveOutputStream.UnicodeExtraFieldPolicy |
createUnicodeExtraFields
whether to create UnicodePathExtraField-s for each entry.
|
private long |
dataStart
Data for local header data
|
(package private) static byte[] |
DD_SIG
data descriptor signature
|
protected java.util.zip.Deflater |
def
This Deflater object is used for output.
|
static int |
DEFAULT_COMPRESSION
Default compression level for deflated entries.
|
(package private) static java.lang.String |
DEFAULT_ENCODING
default encoding for file names and comment.
|
static int |
DEFLATED
Compression method for deflated entries.
|
private static int |
DEFLATER_BLOCK_SIZE |
static int |
EFS_FLAG
Deprecated.
use
GeneralPurposeBit.UFT8_NAMES_FLAG instead |
private java.lang.String |
encoding
The encoding to use for filenames and the file comment.
|
private java.util.List |
entries
List of ZipArchiveEntries written so far.
|
private ZipArchiveEntry |
entry
Current entry.
|
(package private) static byte[] |
EOCD_SIG
end of central dir signature
|
private boolean |
fallbackToUTF8
Whether to encode non-encodable file names as UTF-8.
|
protected boolean |
finished
indicates if this archive is finished.
|
private boolean |
hasCompressionLevelChanged
Has the compression level changed when compared to the last
entry?
|
private int |
level
Compression level for next entry.
|
(package private) static byte[] |
LFH_SIG
local file header signature
|
private long |
localDataStart
Offset for CRC entry in the local file header data for the
current entry starts here.
|
private static byte[] |
LZERO
Helper, a 0 as ZipLong.
|
private int |
method
Default compression method for next entry.
|
private java.util.Map |
offsets
Holds the offsets of the LFH starts for each entry.
|
private java.io.OutputStream |
out |
private java.io.RandomAccessFile |
raf
Optional random access output.
|
private static int |
SHORT |
static int |
STORED
Compression method for stored entries.
|
private boolean |
useUTF8Flag
whether to use the general purpose bit flag when writing UTF-8
filenames or not.
|
private static int |
WORD |
private long |
written
Count the bytes written to out.
|
private static byte[] |
ZERO
Helper, a 0 as ZipShort.
|
private ZipEncoding |
zipEncoding
The zip encoding to use for filenames and the file comment.
|
Constructor and Description |
---|
ZipArchiveOutputStream(java.io.File file)
Creates a new ZIP OutputStream writing to a File.
|
ZipArchiveOutputStream(java.io.OutputStream out)
Creates a new ZIP OutputStream filtering the underlying stream.
|
Modifier and Type | Method and Description |
---|---|
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(java.io.File inputFile,
java.lang.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.
|
private void |
deflateUntilInputIsNeeded() |
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.
|
java.lang.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(java.lang.String comment)
Set the file comment.
|
void |
setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b)
Whether to create Unicode Extra Fields.
|
void |
setEncoding(java.lang.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.
|
private void |
writeVersionNeededToExtractAndGeneralPurposeBits(int zipMethod,
boolean utfFallback) |
count, count, getBytesWritten, getCount, write
static final int BYTE_MASK
private static final int SHORT
private static final int WORD
static final int BUFFER_SIZE
protected boolean finished
private static final int DEFLATER_BLOCK_SIZE
public static final int DEFLATED
public static final int DEFAULT_COMPRESSION
public static final int STORED
static final java.lang.String DEFAULT_ENCODING
public static final int EFS_FLAG
GeneralPurposeBit.UFT8_NAMES_FLAG
insteadprivate ZipArchiveEntry entry
private java.lang.String comment
private int level
private boolean hasCompressionLevelChanged
private int method
private final java.util.List entries
private final java.util.zip.CRC32 crc
private long written
private long dataStart
private long localDataStart
private long cdOffset
private long cdLength
private static final byte[] ZERO
private static final byte[] LZERO
private final java.util.Map offsets
private java.lang.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.
private ZipEncoding zipEncoding
setEncoding(String)
.protected final java.util.zip.Deflater def
private final byte[] buf
private final java.io.RandomAccessFile raf
private final java.io.OutputStream out
private boolean useUTF8Flag
private boolean fallbackToUTF8
private ZipArchiveOutputStream.UnicodeExtraFieldPolicy createUnicodeExtraFields
static final byte[] LFH_SIG
static final byte[] DD_SIG
static final byte[] CFH_SIG
static final byte[] EOCD_SIG
public ZipArchiveOutputStream(java.io.OutputStream out)
out
- the outputstream to zippublic ZipArchiveOutputStream(java.io.File file) throws java.io.IOException
file
- the file to zip tojava.io.IOException
- on errorpublic 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(java.lang.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 java.lang.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 java.io.IOException
finish
in class ArchiveOutputStream
java.io.IOException
public void closeArchiveEntry() throws java.io.IOException
closeArchiveEntry
in class ArchiveOutputStream
java.io.IOException
- on errorpublic void putArchiveEntry(ArchiveEntry archiveEntry) throws java.io.IOException
ArchiveOutputStream.closeArchiveEntry()
to complete the process.putArchiveEntry
in class ArchiveOutputStream
archiveEntry
- describes the entryjava.lang.ClassCastException
- if entry is not an instance of ZipArchiveEntryjava.io.IOException
public void setComment(java.lang.String comment)
comment
- the commentpublic void setLevel(int level)
Default is Deflater.DEFAULT_COMPRESSION.
level
- the compression level.java.lang.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 java.io.IOException
write
in class java.io.OutputStream
b
- the byte array to writeoffset
- the start position to write fromlength
- the number of bytes to writejava.io.IOException
- on errorpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in class java.io.OutputStream
java.io.IOException
- if an I/O error occurs.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- if an I/O error occurs.protected final void deflate() throws java.io.IOException
java.io.IOException
- on errorprotected void writeLocalFileHeader(ZipArchiveEntry ze) throws java.io.IOException
ze
- the entry to writejava.io.IOException
- on errorprotected void writeDataDescriptor(ZipArchiveEntry ze) throws java.io.IOException
ze
- the entry to writejava.io.IOException
- on errorprotected void writeCentralFileHeader(ZipArchiveEntry ze) throws java.io.IOException
ze
- the entry to writejava.io.IOException
- on errorprotected void writeCentralDirectoryEnd() throws java.io.IOException
java.io.IOException
- on errorprotected final void writeOut(byte[] data) throws java.io.IOException
data
- the byte array to writejava.io.IOException
- on errorprotected final void writeOut(byte[] data, int offset, int length) throws java.io.IOException
data
- the byte array to writeoffset
- the start position to write fromlength
- the number of bytes to writejava.io.IOException
- on errorprivate void deflateUntilInputIsNeeded() throws java.io.IOException
java.io.IOException
private void writeVersionNeededToExtractAndGeneralPurposeBits(int zipMethod, boolean utfFallback) throws java.io.IOException
java.io.IOException
public ArchiveEntry createArchiveEntry(java.io.File inputFile, java.lang.String entryName) throws java.io.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 ArchiveOutputStream
java.io.IOException