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.Fields inherited from class java.util.zip.DeflaterOutputStream
buf, defFields 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. -
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.Methods inherited from class java.util.zip.DeflaterOutputStream
deflate, flush, writeMethods inherited from class java.io.OutputStream
write
-
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
static final long LOCSIG- See Also:
- Constant Field Values
-
EXTSIG
static final long EXTSIG- See Also:
- Constant Field Values
-
CENSIG
static final long CENSIG- See Also:
- Constant Field Values
-
ENDSIG
static final long ENDSIG- See Also:
- Constant Field Values
-
LOCHDR
static final int LOCHDR- See Also:
- Constant Field Values
-
EXTHDR
static final int EXTHDR- See Also:
- Constant Field Values
-
CENHDR
static final int CENHDR- See Also:
- Constant Field Values
-
ENDHDR
static final int ENDHDR- See Also:
- Constant Field Values
-
LOCVER
static final int LOCVER- See Also:
- Constant Field Values
-
LOCFLG
static final int LOCFLG- See Also:
- Constant Field Values
-
LOCHOW
static final int LOCHOW- See Also:
- Constant Field Values
-
LOCTIM
static final int LOCTIM- See Also:
- Constant Field Values
-
LOCCRC
static final int LOCCRC- See Also:
- Constant Field Values
-
LOCSIZ
static final int LOCSIZ- See Also:
- Constant Field Values
-
LOCLEN
static final int LOCLEN- See Also:
- Constant Field Values
-
LOCNAM
static final int LOCNAM- See Also:
- Constant Field Values
-
LOCEXT
static final int LOCEXT- See Also:
- Constant Field Values
-
EXTCRC
static final int EXTCRC- See Also:
- Constant Field Values
-
EXTSIZ
static final int EXTSIZ- See Also:
- Constant Field Values
-
EXTLEN
static final int EXTLEN- See Also:
- Constant Field Values
-
CENVEM
static final int CENVEM- See Also:
- Constant Field Values
-
CENVER
static final int CENVER- See Also:
- Constant Field Values
-
CENFLG
static final int CENFLG- See Also:
- Constant Field Values
-
CENHOW
static final int CENHOW- See Also:
- Constant Field Values
-
CENTIM
static final int CENTIM- See Also:
- Constant Field Values
-
CENCRC
static final int CENCRC- See Also:
- Constant Field Values
-
CENSIZ
static final int CENSIZ- See Also:
- Constant Field Values
-
CENLEN
static final int CENLEN- See Also:
- Constant Field Values
-
CENNAM
static final int CENNAM- See Also:
- Constant Field Values
-
CENEXT
static final int CENEXT- See Also:
- Constant Field Values
-
CENCOM
static final int CENCOM- See Also:
- Constant Field Values
-
CENDSK
static final int CENDSK- See Also:
- Constant Field Values
-
CENATT
static final int CENATT- See Also:
- Constant Field Values
-
CENATX
static final int CENATX- See Also:
- Constant Field Values
-
CENOFF
static final int CENOFF- See Also:
- Constant Field Values
-
ENDSUB
static final int ENDSUB- See Also:
- Constant Field Values
-
ENDTOT
static final int ENDTOT- See Also:
- Constant Field Values
-
ENDSIZ
static final int ENDSIZ- See Also:
- Constant Field Values
-
ENDOFF
static final int ENDOFF- See Also:
- Constant Field Values
-
ENDCOM
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
-