Package java.util.zip
Class ZipOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.util.zip.DeflaterOutputStream
-
- java.util.zip.ZipOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
JarOutputStream
public class ZipOutputStream extends DeflaterOutputStream
Used to write (compress) data into zip files.ZipOutputStreamis used to writeZipEntrys to the underlying stream. Output fromZipOutputStreamcan be read usingZipFileorZipInputStream.While
DeflaterOutputStreamcan write compressed zip file entries, this extension can write uncompressed entries as well. UseZipEntry.setMethod(int)orsetMethod(int)with theZipEntry.STOREDflag.Example
Using
ZipOutputStreamis a little more complicated thanGZIPOutputStreambecause zip files are containers that can contain multiple files. This code creates a zip file containing several files, similar to thezip(1)utility.OutputStream os = ... ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os)); try { for (int i = 0; i < fileCount; ++i) { String filename = ... byte[] bytes = ... ZipEntry entry = new ZipEntry(filename); zos.putNextEntry(entry); zos.write(bytes); zos.closeEntry(); } } finally { zos.close(); }
-
-
Field Summary
Fields Modifier and Type Field Description static intCENATTstatic intCENATXstatic intCENCOMstatic intCENCRCstatic intCENDSKstatic intCENEXTstatic intCENFLGstatic intCENHDRstatic intCENHOWstatic intCENLENstatic intCENNAMstatic intCENOFFstatic longCENSIGstatic intCENSIZstatic intCENTIMstatic intCENVEMstatic intCENVERstatic intDEFLATEDIndicates deflated entries.static intENDCOMstatic intENDHDRstatic intENDOFFstatic longENDSIGstatic intENDSIZstatic intENDSUBstatic intENDTOTstatic intEXTCRCstatic intEXTHDRstatic intEXTLENstatic longEXTSIGstatic intEXTSIZstatic intLOCCRCstatic intLOCEXTstatic intLOCFLGstatic intLOCHDRstatic intLOCHOWstatic intLOCLENstatic intLOCNAMstatic longLOCSIGstatic intLOCSIZstatic intLOCTIMstatic intLOCVERstatic intSTOREDIndicates uncompressed entries.-
Fields inherited from class java.util.zip.DeflaterOutputStream
buf, def
-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description ZipOutputStream(OutputStream os)Constructs a newZipOutputStreamthat writes a zip file to the givenOutputStream.ZipOutputStream(OutputStream os, boolean forceZip64)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the currentZipEntry, if any, and the underlying output stream.voidcloseEntry()Closes the currentZipEntry.voidfinish()Indicates that all entries have been written to the stream.voidputNextEntry(ZipEntry ze)Writes entry information to the underlying stream.voidsetComment(String comment)Sets the comment associated with the file being written.voidsetLevel(int level)Sets the compression level to be used for writing entry data.voidsetMethod(int method)Sets the default compression method to be used when aZipEntrydoesn't explicitly specify a method.voidwrite(byte[] buffer, int offset, int byteCount)Writes data for the current entry to the underlying stream.-
Methods inherited from class java.util.zip.DeflaterOutputStream
deflate, flush, write
-
Methods inherited from class java.io.OutputStream
write
-
-
-
-
Field Detail
-
DEFLATED
public static final int DEFLATED
Indicates deflated entries.- See Also:
- Constant Field Values
-
STORED
public static final int STORED
Indicates uncompressed entries.- See Also:
- Constant Field Values
-
LOCSIG
public static final long LOCSIG
- See Also:
- Constant Field Values
-
EXTSIG
public static final long EXTSIG
- See Also:
- Constant Field Values
-
CENSIG
public static final long CENSIG
- See Also:
- Constant Field Values
-
ENDSIG
public static final long ENDSIG
- See Also:
- Constant Field Values
-
LOCHDR
public static final int LOCHDR
- See Also:
- Constant Field Values
-
EXTHDR
public static final int EXTHDR
- See Also:
- Constant Field Values
-
CENHDR
public static final int CENHDR
- See Also:
- Constant Field Values
-
ENDHDR
public static final int ENDHDR
- See Also:
- Constant Field Values
-
LOCVER
public static final int LOCVER
- See Also:
- Constant Field Values
-
LOCFLG
public static final int LOCFLG
- See Also:
- Constant Field Values
-
LOCHOW
public static final int LOCHOW
- See Also:
- Constant Field Values
-
LOCTIM
public static final int LOCTIM
- See Also:
- Constant Field Values
-
LOCCRC
public static final int LOCCRC
- See Also:
- Constant Field Values
-
LOCSIZ
public static final int LOCSIZ
- See Also:
- Constant Field Values
-
LOCLEN
public static final int LOCLEN
- See Also:
- Constant Field Values
-
LOCNAM
public static final int LOCNAM
- See Also:
- Constant Field Values
-
LOCEXT
public static final int LOCEXT
- See Also:
- Constant Field Values
-
EXTCRC
public static final int EXTCRC
- See Also:
- Constant Field Values
-
EXTSIZ
public static final int EXTSIZ
- See Also:
- Constant Field Values
-
EXTLEN
public static final int EXTLEN
- See Also:
- Constant Field Values
-
CENVEM
public static final int CENVEM
- See Also:
- Constant Field Values
-
CENVER
public static final int CENVER
- See Also:
- Constant Field Values
-
CENFLG
public static final int CENFLG
- See Also:
- Constant Field Values
-
CENHOW
public static final int CENHOW
- See Also:
- Constant Field Values
-
CENTIM
public static final int CENTIM
- See Also:
- Constant Field Values
-
CENCRC
public static final int CENCRC
- See Also:
- Constant Field Values
-
CENSIZ
public static final int CENSIZ
- See Also:
- Constant Field Values
-
CENLEN
public static final int CENLEN
- See Also:
- Constant Field Values
-
CENNAM
public static final int CENNAM
- See Also:
- Constant Field Values
-
CENEXT
public static final int CENEXT
- See Also:
- Constant Field Values
-
CENCOM
public static final int CENCOM
- See Also:
- Constant Field Values
-
CENDSK
public static final int CENDSK
- See Also:
- Constant Field Values
-
CENATT
public static final int CENATT
- See Also:
- Constant Field Values
-
CENATX
public static final int CENATX
- See Also:
- Constant Field Values
-
CENOFF
public static final int CENOFF
- See Also:
- Constant Field Values
-
ENDSUB
public static final int ENDSUB
- See Also:
- Constant Field Values
-
ENDTOT
public static final int ENDTOT
- See Also:
- Constant Field Values
-
ENDSIZ
public static final int ENDSIZ
- See Also:
- Constant Field Values
-
ENDOFF
public static final int ENDOFF
- See Also:
- Constant Field Values
-
ENDCOM
public static final int ENDCOM
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ZipOutputStream
public ZipOutputStream(OutputStream os)
Constructs a newZipOutputStreamthat writes a zip file to the givenOutputStream.UTF-8 will be used to encode the file comment, entry names and comments.
-
ZipOutputStream
public ZipOutputStream(OutputStream os, boolean forceZip64)
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses the currentZipEntry, if any, and the underlying output stream. If the stream is already closed this method does nothing.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classDeflaterOutputStream- Throws:
IOException- If an error occurs closing the stream.
-
closeEntry
public void closeEntry() throws IOExceptionCloses the currentZipEntry. Any entry terminal data is written to the underlying stream.- Throws:
IOException- If an error occurs closing the entry.
-
finish
public void finish() throws IOExceptionIndicates that all entries have been written to the stream. Any terminal information is written to the underlying stream.- Overrides:
finishin classDeflaterOutputStream- Throws:
IOException- if an error occurs while terminating the stream.
-
putNextEntry
public void putNextEntry(ZipEntry ze) throws IOException
Writes entry information to the underlying stream. Data associated with the entry can then be written usingwrite(). After data is writtencloseEntry()must be called to complete the writing of the entry to the underlying stream.- Parameters:
ze- theZipEntryto store.- Throws:
IOException- If an error occurs storing the entry.- See Also:
write(byte[], int, int)
-
setComment
public void setComment(String comment)
Sets the comment associated with the file being written. SeeZipFile.getComment().- Throws:
IllegalArgumentException- if the comment is >= 64 Ki encoded bytes.
-
setLevel
public void setLevel(int level)
Sets the compression level to be used for writing entry data.
-
setMethod
public void setMethod(int method)
Sets the default compression method to be used when aZipEntrydoesn't explicitly specify a method. SeeZipEntry.setMethod(int)for more details.
-
write
public void write(byte[] buffer, int offset, int byteCount) throws IOExceptionWrites data for the current entry to the underlying stream.- Overrides:
writein classDeflaterOutputStream- Parameters:
buffer- the buffer to write.offset- the index of the first byte inbufferto write.byteCount- the number of bytes inbufferto write.- Throws:
IOException- If an error occurs writing to the stream
-
-