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.
ZipOutputStream is used to write ZipEntrys to the underlying
stream. Output from ZipOutputStream can be read using ZipFile
or ZipInputStream.
While DeflaterOutputStream can write compressed zip file
entries, this extension can write uncompressed entries as well.
Use ZipEntry.setMethod(int) or setMethod(int) with the ZipEntry.STORED flag.
Example
Using ZipOutputStream is a little more complicated than GZIPOutputStream
because zip files are containers that can contain multiple files. This code creates a zip
file containing several files, similar to the zip(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. -
Constructor Summary
Constructors Constructor Description ZipOutputStream(OutputStream os)Constructs a newZipOutputStreamthat writes a zip file to the givenOutputStream. -
Method Summary
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.
-
Field Details
-
DEFLATED
public static final int DEFLATEDIndicates deflated entries.- See Also:
- Constant Field Values
-
STORED
public static final int STOREDIndicates 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 Details
-
ZipOutputStream
Constructs a newZipOutputStreamthat writes a zip file to the givenOutputStream.
-
-
Method Details
-
close
Closes 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
Closes the currentZipEntry. Any entry terminal data is written to the underlying stream.- Throws:
IOException- If an error occurs closing the entry.
-
finish
Indicates 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
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
Sets the comment associated with the file being written. SeeZipFile.getComment().- Throws:
IllegalArgumentException- if the comment is >= 64 Ki UTF-8 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
Writes 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
-