-
- All Implemented Interfaces:
-
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class ZipArchiveOutputStream extends ArchiveOutputStream
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 when it knows that the output is going to go to a file and no split archive shall be created.
If SeekableByteChannel 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 can be called.
As of Apache Commons Compress 1.3 it transparently supports Zip64 extensions and thus individual entries and archives larger than 4 GB or with more than 65536 entries in most cases but explicit control is provided via setUseZip64. If the stream can not use SeekableByteChannel and you try to write a ZipArchiveEntry of unknown size then Zip64 extensions will be disabled by default.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classZipArchiveOutputStream.UnicodeExtraFieldPolicyenum that represents the possible policies for creating Unicodeextra fields.
-
Field Summary
Fields Modifier and Type Field Description public final static intDEFLATEDpublic final static intDEFAULT_COMPRESSIONpublic final static intSTOREDpublic final static intEFS_FLAGprivate Stringcommentprivate intlevelprivate intmethodprivate Stringencodingprivate booleanfallbackToUTF8private ZipArchiveOutputStream.UnicodeExtraFieldPolicycreateUnicodeExtraFields
-
Constructor Summary
Constructors Constructor Description ZipArchiveOutputStream(OutputStream out)Creates a new ZIP OutputStream filtering the underlying stream. ZipArchiveOutputStream(File file, long zipSplitSize)Creates a split ZIP Archive. ZipArchiveOutputStream(SeekableByteChannel channel)Creates a new ZIP OutputStream writing to a SeekableByteChannel.
-
Method Summary
Modifier and Type Method Description voidsetComment(String comment)Set the file comment. voidsetLevel(int level)Sets the compression level for subsequent entries. voidsetMethod(int method)Sets the default compression method for subsequent entries. StringgetEncoding()The encoding to use for file names and the file comment. voidsetEncoding(String encoding)The encoding to use for file names and the file comment. voidsetFallbackToUTF8(boolean b)Whether to fall back to UTF and the language encoding flag ifthe file name cannot be encoded using the specified encoding. voidsetCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b)Whether to create Unicode Extra Fields. voidaddRawArchiveEntry(ZipArchiveEntry entry, InputStream rawStream)Adds an archive entry with a raw input stream.If crc, size and compressed size are supplied on the entry, these values will be used as-is.Zip64 status is re-established based on the settings in this stream, and the supplied valueis ignored.The entry is put and closed immediately. booleancanWriteEntryData(ArchiveEntry ae)Whether this stream is able to write the given entry. voidclose()Closes this output stream and releases any system resourcesassociated with the stream. voidcloseArchiveEntry()Writes all necessary data for this entry. ArchiveEntrycreateArchiveEntry(File inputFile, String entryName)Creates a new zip entry taking some information from the givenfile and using the provided name. voidfinish()Finishes the addition of entries to this stream, without closing it.Additional data can be written, if the format supports it. voidflush()Flushes this output stream and forces any buffered output bytesto be written out to the stream. longgetBytesWritten()Returns the total number of bytes written to this stream. booleanisSeekable()This method indicates whether this archive is writing to aseekable stream (i.e., to a random access file). voidputArchiveEntry(ArchiveEntry archiveEntry)Writes the headers for an archive entry to the output stream.The caller must then write the content to the stream and call #closeArchiveEntry() to complete the process. voidsetUseLanguageEncodingFlag(boolean b)Whether to set the language encoding flag if the file nameencoding is UTF-8. voidsetUseZip64(Zip64Mode mode)Whether Zip64 extensions will be used. voidwrite(Array<byte> b, int offset, int length)Writes bytes to ZIP entry. voidwritePreamble(Array<byte> preamble)Write preamble data. voidwritePreamble(Array<byte> preamble, int offset, int length)Write preamble data. -
Methods inherited from class org.readium.r2.shared.util.zip.compress.archivers.ArchiveOutputStream
canWriteEntryData, createArchiveEntry, getCount, putArchiveEntry, write -
Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
ZipArchiveOutputStream
ZipArchiveOutputStream(OutputStream out)
Creates a new ZIP OutputStream filtering the underlying stream.- Parameters:
out- the outputstream to zip
-
ZipArchiveOutputStream
ZipArchiveOutputStream(File file, long zipSplitSize)
Creates a split ZIP Archive.- Parameters:
file- the file that will become the last part of the split archivezipSplitSize- maximum size of a single part of the splitarchive created by this stream.
-
ZipArchiveOutputStream
ZipArchiveOutputStream(SeekableByteChannel channel)
Creates a new ZIP OutputStream writing to a SeekableByteChannel.- Parameters:
channel- the channel to zip to
-
-
Method Detail
-
setComment
void setComment(String comment)
Set the file comment.
- Parameters:
comment- the comment
-
setLevel
void setLevel(int level)
Sets the compression level for subsequent entries.
Default is Deflater.DEFAULT_COMPRESSION.
- Parameters:
level- the compression level.
-
setMethod
void setMethod(int method)
Sets the default compression method for subsequent entries.
Default is DEFLATED.
- Parameters:
method- an{@code int}from java.util.zip.
-
getEncoding
String getEncoding()
The encoding to use for file names and the file comment.
-
setEncoding
void setEncoding(String encoding)
The encoding to use for file names and the file comment.
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.
- Parameters:
encoding- the encoding to use for file names, use nullfor the platform's default encoding
-
setFallbackToUTF8
void setFallbackToUTF8(boolean b)
Whether to fall back to UTF and the language encoding flag ifthe file name cannot be encoded using the specified encoding.
Defaults to false.
- Parameters:
b- whether to fall back to UTF and the language encodingflag if the file name cannot be encoded using the specifiedencoding.
-
setCreateUnicodeExtraFields
void setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b)
Whether to create Unicode Extra Fields.
Defaults to NEVER.
- Parameters:
b- whether to create Unicode Extra Fields.
-
addRawArchiveEntry
void addRawArchiveEntry(ZipArchiveEntry entry, InputStream rawStream)
Adds an archive entry with a raw input stream.If crc, size and compressed size are supplied on the entry, these values will be used as-is.Zip64 status is re-established based on the settings in this stream, and the supplied valueis ignored.The entry is put and closed immediately.
- Parameters:
entry- The archive entry to addrawStream- The raw input stream of a different entry.
-
canWriteEntryData
boolean canWriteEntryData(ArchiveEntry ae)
Whether this stream is able to write the given entry.
May return false if it is set up to use encryption or acompression method that hasn't been implemented yet.
-
close
void close()
Closes this output stream and releases any system resourcesassociated with the stream.
-
closeArchiveEntry
void closeArchiveEntry()
Writes all necessary data for this entry.
-
createArchiveEntry
ArchiveEntry createArchiveEntry(File inputFile, String entryName)
Creates a new zip entry taking some information from the givenfile and using the provided name.
The name will be adjusted to end with a forward slash "/" ifthe file is a directory. If the file is not a directory apotential trailing forward slash will be stripped from theentry name.
Must not be used if the stream has already been closed.
-
finish
void finish()
Finishes the addition of entries to this stream, without closing it.Additional data can be written, if the format supports it.
-
flush
void flush()
Flushes this output stream and forces any buffered output bytesto be written out to the stream.
-
getBytesWritten
long getBytesWritten()
Returns the total number of bytes written to this stream.
-
isSeekable
boolean isSeekable()
This method indicates whether this archive is writing to aseekable stream (i.e., to a random access file).
For seekable streams, you don't need to calculate the CRC oruncompressed size for STORED entries beforeinvoking putArchiveEntry.
-
putArchiveEntry
void putArchiveEntry(ArchiveEntry archiveEntry)
Writes the headers for an archive entry to the output stream.The caller must then write the content to the stream and call #closeArchiveEntry() to complete the process.
-
setUseLanguageEncodingFlag
void setUseLanguageEncodingFlag(boolean b)
Whether to set the language encoding flag if the file nameencoding is UTF-8.
Defaults to true.
- Parameters:
b- whether to set the language encoding flag if the filename encoding is UTF-8
-
setUseZip64
void setUseZip64(Zip64Mode mode)
Whether Zip64 extensions will be used.
When setting the mode to Never, putArchiveEntry, closeArchiveEntry, or close may throw a if the entry's size or the total sizeof the archive exceeds 4GB or there are more than 65536 entriesinside the archive. Any archive created in this mode will bereadable by implementations that don't support Zip64.
When setting the mode to Always,Zip64 extensions will be used for all entries. Any archivecreated in this mode may be unreadable by implementations thatdon't support Zip64 even if all its contents would be.
When setting the mode to AsNeeded, Zip64 extensions will transparently be used forthose entries that require them. This mode can only be used ifthe uncompressed size of the ZipArchiveEntry is knownwhen calling putArchiveEntry -this mode is not valid when the output stream is not seekableand the uncompressed size is unknown when is called.
If no entry inside the resulting archive requires Zip64extensions then Never will create thesmallest archive. AsNeeded willcreate a slightly bigger archive if the uncompressed size ofany entry has initially been unknown and create an archiveidentical to Never otherwise. Always will create an archive that is atleast 24 bytes per entry bigger than the one Never would create.
Defaults to AsNeeded unless putArchiveEntry is called with an entry of unknownsize and data is written to a non-seekable stream - in thiscase the default is Never.
- Parameters:
mode- Whether Zip64 extensions will be used.
-
write
void write(Array<byte> b, int offset, int length)
Writes bytes to ZIP entry.
- Parameters:
b- the byte array to writeoffset- the start position to write fromlength- the number of bytes to write
-
writePreamble
void writePreamble(Array<byte> preamble)
Write preamble data. For most of time, this is used tomake self-extracting zips.
- Parameters:
preamble- data to write
-
writePreamble
void writePreamble(Array<byte> preamble, int offset, int length)
Write preamble data. For most of time, this is used tomake self-extracting zips.
- Parameters:
preamble- data to writeoffset- the start offset in the datalength- the number of bytes to write
-
-
-
-