- All Implemented Interfaces:
Closeable,AutoCloseable
The builder writes entries sequentially to the archive.
Each file entry returns an OutputStream that must be closed before another entry can be added.
Closing the builder writes the central directory and end-of-central-directory records to complete the archive.
The archive is written using a BufferedFile, which allows random-access patching of CRC-32
and size fields in local file headers after entry data has been written.
All file names are encoded as UTF-8 (general purpose bit 11 is always set).
Usage example
try (ArchiveBuilder builder = ArchiveBuilder.open(path)) {
builder.addDirectory("META-INF/");
try (OutputStream os = builder.addEntry("META-INF/MANIFEST.MF", Set.of(ZipOption.STORED))) {
os.write(manifestBytes);
}
try (OutputStream os = builder.addEntry("com/example/Main.class")) {
os.write(classBytes);
}
}
Compression
The compression method can be set at the builder level (as a default for all entries) and/or per entry. Per-entry options override the builder default. If no compression option is specified at either level, the default isZipOption.DEFLATED.
ZIP64 support
By default, entries are written in standard ZIP format. If any single entry may exceed 4 GB, passZipOption.ZIP64 to the open(Path, Collection, FileAttribute[]) factory method
(as a builder-level default) or to
addEntry(String, Collection, FileAttribute[]) (for individual entries)
to reserve ZIP64 extended information in the local file header.
The central directory and end-of-central-directory records will use ZIP64 format when necessary,
regardless of this option.
Thread safety
This class is not thread-safe.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddArchive(String name) Add a nested archive as a STORED entry in this archive.addArchive(String name, FileAttribute<?>... attrs) Add a nested archive as a STORED entry in this archive with the given file attributes.addArchive(String name, OpenOption... options) Add a nested archive as a STORED entry in this archive with the given options.addArchive(String name, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a nested archive as a STORED entry in this archive with the given options and file attributes.addBufferedEntry(String name) Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream, using the builder's default options and no file attributes.addBufferedEntry(String name, FileAttribute<?>... attrs) Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream.addBufferedEntry(String name, OpenOption... options) Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream.addBufferedEntry(String name, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream.voidaddDirectory(String name, FileAttribute<?>... attrs) Add a directory entry to the archive.Add a file entry to the archive using the builder's default options and no file attributes.voidAdd a file entry to the archive with the content from the given byte array, using the builder's default options and no file attributes.voidAdd a file entry to the archive with the content from a region of the given byte array, using the builder's default options and no file attributes.voidaddEntry(String name, byte[] content, int offset, int length, FileAttribute<?>... attrs) Add a file entry to the archive with the content from a region of the given byte array, with the given file attributes.voidaddEntry(String name, byte[] content, int offset, int length, OpenOption... options) Add a file entry to the archive with the content from a region of the given byte array, with the given options.voidaddEntry(String name, byte[] content, int offset, int length, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the content from a region of the given byte array, with the given options and file attributes.voidaddEntry(String name, byte[] content, FileAttribute<?>... attrs) Add a file entry to the archive with the content from the given byte array, with the given file attributes.voidaddEntry(String name, byte[] content, OpenOption... options) Add a file entry to the archive with the content from the given byte array, with the given options.voidaddEntry(String name, byte[] content, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the content from the given byte array, with the given options and file attributes.voidaddEntry(String name, InputStream content) Add a file entry to the archive with the content read from the given input stream, using the builder's default options and no file attributes.voidaddEntry(String name, InputStream content, FileAttribute<?>... attrs) Add a file entry to the archive with the content read from the given input stream and the given file attributes.voidaddEntry(String name, InputStream content, OpenOption... options) Add a file entry to the archive with the content read from the given input stream and the given options.voidaddEntry(String name, InputStream content, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the content read from the given input stream, with the given options and file attributes.voidAdd a file entry to the archive with the text content read from the given reader, using the builder's default options and no file attributes.voidaddEntry(String name, Reader content, Charset charset, FileAttribute<?>... attrs) Add a file entry to the archive with the text content read from the given reader, with the given file attributes.voidaddEntry(String name, Reader content, Charset charset, OpenOption... options) Add a file entry to the archive with the text content read from the given reader, with the given options.voidaddEntry(String name, Reader content, Charset charset, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the text content read from the given reader, with the given options and file attributes.voidaddEntry(String name, CharSequence content, Charset charset) Add a file entry to the archive with the text content read from the given character sequence, using the builder's default options and no file attributes.voidaddEntry(String name, CharSequence content, Charset charset, FileAttribute<?>... attrs) Add a file entry to the archive with the text content read from the given character sequence, with the given file attributes.voidaddEntry(String name, CharSequence content, Charset charset, OpenOption... options) Add a file entry to the archive with the text content read from the given character sequence, with the given options.voidaddEntry(String name, CharSequence content, Charset charset, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the text content read from the given character sequence, with the given options and file attributes.Add a file entry to the archive and return aWriterfor writing text content, using the builder's default options and no file attributes.addEntry(String name, Charset charset, FileAttribute<?>... attrs) Add a file entry to the archive and return aWriterfor writing text content, with the given file attributes.addEntry(String name, Charset charset, OpenOption... options) Add a file entry to the archive and return aWriterfor writing text content, with the given options.addEntry(String name, Charset charset, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive and return aWriterfor writing text content, with the given options and file attributes.addEntry(String name, FileAttribute<?>... attrs) Add a file entry to the archive with the given options.addEntry(String name, OpenOption... options) Add a file entry to the archive with the given options and no file attributes.voidAdd a file entry to the archive with the content copied from the given path, using the builder's default options and no file attributes.voidaddEntry(String name, Path content, FileAttribute<?>... attrs) Add a file entry to the archive with the content copied from the given path, with the given file attributes.voidaddEntry(String name, Path content, OpenOption... options) Add a file entry to the archive with the content copied from the given path, with the given options.voidaddEntry(String name, Path content, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the content copied from the given path, with the given options and file attributes.addEntry(String name, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Add a file entry to the archive with the given options.Begin writing raw (non-entry) data to the archive file and return anOutputStreamfor the data.voidaddRawData(byte[] data) Write raw (non-entry) data to the archive file from the given byte array.voidaddRawData(byte[] data, int offset, int length) Write raw (non-entry) data to the archive file from a region of the given byte array.voidaddRawData(InputStream content) Write raw (non-entry) data to the archive file, reading all bytes from the given input stream.voidclose()Close this archive builder, writing the central directory and end-of-central-directory records.static ArchiveBuilderopen(BufferedFile file, OpenOption... options) Open a new archive builder for writing to an existingBufferedFile.static ArchiveBuilderopen(BufferedFile file, Collection<? extends OpenOption> options) Open a new archive builder for writing to an existingBufferedFile.static ArchiveBuilderOpen a new archive builder for writing to the given path.static ArchiveBuilderopen(Path path, FileAttribute<?>... attrs) Open a new archive builder for writing to the given path.static ArchiveBuilderopen(Path path, OpenOption... options) Open a new archive builder for writing to the given path, with the given options.static ArchiveBuilderopen(Path path, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) Open a new archive builder for writing to the given path, with the given options.longposition()Returns the current position within the archive, in bytes.
-
Method Details
-
open
Open a new archive builder for writing to the given path. The file is created if it does not exist. If the file already exists, its previous contents are overwritten. This is equivalent to callingopen(Path, Collection, FileAttribute...)with an empty options set.- Parameters:
path- the path of the archive file to create (must not benull)- Returns:
- a new archive builder (not
null) - Throws:
IOException- if an I/O error occurs
-
open
Open a new archive builder for writing to the given path. The file is created if it does not exist. If the file already exists, its previous contents are overwritten. The optional file attributes are applied to the created file on the filesystem. This is equivalent to callingopen(Path, Collection, FileAttribute...)with an empty options set.- Parameters:
path- the path of the archive file to create (must not benull)attrs- optional file attributes to set on the created file- Returns:
- a new archive builder (not
null) - Throws:
IOException- if an I/O error occurs
-
open
Open a new archive builder for writing to the given path, with the given options. Seeopen(Path, Collection, FileAttribute...)for details on how options are handled.- Parameters:
path- the path of the archive file to create (must not benull)options- the open options (must not benull); may containZipOptionvalues and/orStandardOpenOptionvalues- Returns:
- a new archive builder (not
null) - Throws:
IOException- if an I/O error occursUnsupportedOperationException- if an unsupported option is specified
-
open
public static ArchiveBuilder open(Path path, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Open a new archive builder for writing to the given path, with the given options. The file is always opened for both reading and writing. By default, the file is created if it does not exist, and its previous contents (if any) are overwritten.The following
StandardOpenOptionvalues are accepted:CREATE_NEW— create the file atomically; an exception is thrown if the file already existsTRUNCATE_EXISTING— truncate the file to zero length when opened (this is the default behavior whenCREATE_NEWis not given)CREATE,READ,WRITE— accepted silently (these behaviors are always implied)
ZipOptionvalues may also be specified to set builder-level defaults. The optional file attributes are applied to the created file on the filesystem.- Parameters:
path- the path of the archive file to create (must not benull)options- the open options (must not benull); may containZipOptionvalues and/orStandardOpenOptionvaluesattrs- optional file attributes to set on the created file- Returns:
- a new archive builder (not
null) - Throws:
IOException- if an I/O error occursUnsupportedOperationException- if an unsupported option is specified
-
open
Open a new archive builder for writing to an existingBufferedFile. The file must be open for both reading and writing. When the builder is closed, the central directory is written but theBufferedFileitself is not closed. TheBufferedFilemay not be used until the builder is closed, because the builder opens a nested file on it.ZipOptionvalues may be specified to set builder-level defaults.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since the file is already open.- Parameters:
file- the buffered file to write the archive to (must not benull)options- the options (must not benull); may containZipOptionvalues- Returns:
- a new archive builder (not
null) - Throws:
IOException- if an I/O error occursUnsupportedOperationException- if an unsupported option is specified
-
open
public static ArchiveBuilder open(BufferedFile file, Collection<? extends OpenOption> options) throws IOException Open a new archive builder for writing to an existingBufferedFile. The file must be open for both reading and writing. When the builder is closed, the central directory is written but theBufferedFileitself is not closed.ZipOptionvalues may be specified to set builder-level defaults.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since the file is already open.- Parameters:
file- the buffered file to write the archive to (must not benull)options- the options (must not benull); may containZipOptionvalues- Returns:
- a new archive builder (not
null) - Throws:
IOException- if an I/O error occursUnsupportedOperationException- if an unsupported option is specified
-
position
Returns the current position within the archive, in bytes. The position is relative to the start of the archive data, not to the start of the underlying file. This method may only be called when no entry is currently being written.- Returns:
- the current position within the archive, in bytes
- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addDirectory
Add a directory entry to the archive. A trailing'/'is appended to the name if not already present. Directory entries are always stored with theSTOREDmethod, zero CRC-32, and zero size.- Parameters:
name- the directory name (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
Add a file entry to the archive using the builder's default options and no file attributes. The returned output stream must be closed before another entry can be added or the archive can be closed.- Parameters:
name- the entry name (must not benull)- Returns:
- an output stream to which the entry data should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
Add a file entry to the archive with the given options and no file attributes. Options given here override the builder-level defaults for this entry. The returned output stream must be closed before another entry can be added or the archive can be closed.- Parameters:
name- the entry name (must not benull)options- the entry options; may containZipOptionvalues- Returns:
- an output stream to which the entry data should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the given options. Options given here override the builder-level defaults for this entry. The returned output stream must be closed before another entry can be added or the archive can be closed.- Parameters:
name- the entry name (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Returns:
- an output stream to which the entry data should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
public OutputStream addEntry(String name, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the given options. Options given here override the builder-level defaults for this entry. The returned output stream must be closed before another entry can be added or the archive can be closed.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Returns:
- an output stream to which the entry data should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the content read from the given input stream, using the builder's default options and no file attributes. All bytes are read from the input stream usingInputStream.transferTo(OutputStream). The input stream is not closed by this method.- Parameters:
name- the entry name (must not benull)content- the input stream to read from (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
Add a file entry to the archive with the content read from the given input stream and the given options. All bytes are read from the input stream usingInputStream.transferTo(OutputStream). The input stream is not closed by this method.- Parameters:
name- the entry name (must not benull)content- the input stream to read from (must not benull)options- the entry options; may containZipOptionvalues- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
public void addEntry(String name, InputStream content, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the content read from the given input stream and the given file attributes. All bytes are read from the input stream usingInputStream.transferTo(OutputStream). The input stream is not closed by this method.- Parameters:
name- the entry name (must not benull)content- the input stream to read from (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, InputStream content, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the content read from the given input stream, with the given options and file attributes. All bytes are read from the input stream usingInputStream.transferTo(OutputStream). The input stream is not closed by this method.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)content- the input stream to read from (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the content from the given byte array, using the builder's default options and no file attributes.- Parameters:
name- the entry name (must not benull)content- the byte array to write (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
Add a file entry to the archive with the content from the given byte array, with the given options.- Parameters:
name- the entry name (must not benull)content- the byte array to write (must not benull)options- the entry options; may containZipOptionvalues- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the content from the given byte array, with the given file attributes.- Parameters:
name- the entry name (must not benull)content- the byte array to write (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, byte[] content, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the content from the given byte array, with the given options and file attributes.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)content- the byte array to write (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the content from a region of the given byte array, using the builder's default options and no file attributes.- Parameters:
name- the entry name (must not benull)content- the byte array to write from (must not benull)offset- the start offset in the arraylength- the number of bytes to write- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIndexOutOfBoundsException- ifoffsetorlengthis negative, oroffset + lengthexceeds the array length
-
addEntry
public void addEntry(String name, byte[] content, int offset, int length, OpenOption... options) throws IOException Add a file entry to the archive with the content from a region of the given byte array, with the given options.- Parameters:
name- the entry name (must not benull)content- the byte array to write from (must not benull)offset- the start offset in the arraylength- the number of bytes to writeoptions- the entry options; may containZipOptionvalues- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specifiedIndexOutOfBoundsException- ifoffsetorlengthis negative, oroffset + lengthexceeds the array length
-
addEntry
public void addEntry(String name, byte[] content, int offset, int length, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the content from a region of the given byte array, with the given file attributes.- Parameters:
name- the entry name (must not benull)content- the byte array to write from (must not benull)offset- the start offset in the arraylength- the number of bytes to writeattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIndexOutOfBoundsException- ifoffsetorlengthis negative, oroffset + lengthexceeds the array length
-
addEntry
public void addEntry(String name, byte[] content, int offset, int length, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the content from a region of the given byte array, with the given options and file attributes.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)content- the byte array to write from (must not benull)offset- the start offset in the arraylength- the number of bytes to writeoptions- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specifiedIndexOutOfBoundsException- ifoffsetorlengthis negative, oroffset + lengthexceeds the array length
-
addEntry
Add a file entry to the archive and return aWriterfor writing text content, using the builder's default options and no file attributes. The returned writer wraps anOutputStreamWriterover the entry's output stream. The writer must be closed before another entry can be added or the archive can be closed.- Parameters:
name- the entry name (must not benull)charset- the character set for encoding text (must not benull)- Returns:
- a writer to which the entry text should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
Add a file entry to the archive and return aWriterfor writing text content, with the given options. The returned writer wraps anOutputStreamWriterover the entry's output stream. The writer must be closed before another entry can be added or the archive can be closed.- Parameters:
name- the entry name (must not benull)charset- the character set for encoding text (must not benull)options- the entry options; may containZipOptionvalues- Returns:
- a writer to which the entry text should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive and return aWriterfor writing text content, with the given file attributes. The returned writer wraps anOutputStreamWriterover the entry's output stream. The writer must be closed before another entry can be added or the archive can be closed.- Parameters:
name- the entry name (must not benull)charset- the character set for encoding text (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Returns:
- a writer to which the entry text should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public Writer addEntry(String name, Charset charset, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive and return aWriterfor writing text content, with the given options and file attributes. The returned writer wraps anOutputStreamWriterover the entry's output stream. The writer must be closed before another entry can be added or the archive can be closed.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)charset- the character set for encoding text (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Returns:
- a writer to which the entry text should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the text content read from the given character sequence, using the builder's default options and no file attributes. The character sequence is encoded using the given character set.- Parameters:
name- the entry name (must not benull)content- the character sequence to write (must not benull)charset- the character set for encoding text (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, CharSequence content, Charset charset, OpenOption... options) throws IOException Add a file entry to the archive with the text content read from the given character sequence, with the given options. The character sequence is encoded using the given character set.- Parameters:
name- the entry name (must not benull)content- the character sequence to write (must not benull)charset- the character set for encoding text (must not benull)options- the entry options; may containZipOptionvalues- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
public void addEntry(String name, CharSequence content, Charset charset, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the text content read from the given character sequence, with the given file attributes. The character sequence is encoded using the given character set.- Parameters:
name- the entry name (must not benull)content- the character sequence to write (must not benull)charset- the character set for encoding text (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, CharSequence content, Charset charset, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the text content read from the given character sequence, with the given options and file attributes. The character sequence is encoded using the given character set.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)content- the character sequence to write (must not benull)charset- the character set for encoding text (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the text content read from the given reader, using the builder's default options and no file attributes. All characters are read from the reader and encoded using the given character set. The reader is not closed by this method.- Parameters:
name- the entry name (must not benull)content- the reader to read from (must not benull)charset- the character set for encoding text (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, Reader content, Charset charset, OpenOption... options) throws IOException Add a file entry to the archive with the text content read from the given reader, with the given options. All characters are read from the reader and encoded using the given character set. The reader is not closed by this method.- Parameters:
name- the entry name (must not benull)content- the reader to read from (must not benull)charset- the character set for encoding text (must not benull)options- the entry options; may containZipOptionvalues- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
public void addEntry(String name, Reader content, Charset charset, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the text content read from the given reader, with the given file attributes. All characters are read from the reader and encoded using the given character set. The reader is not closed by this method.- Parameters:
name- the entry name (must not benull)content- the reader to read from (must not benull)charset- the character set for encoding text (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, Reader content, Charset charset, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the text content read from the given reader, with the given options and file attributes. All characters are read from the reader and encoded using the given character set. The reader is not closed by this method.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)content- the reader to read from (must not benull)charset- the character set for encoding text (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the content copied from the given path, using the builder's default options and no file attributes. The file content is copied usingFiles.copy(Path, OutputStream).- Parameters:
name- the entry name (must not benull)content- the path of the file to copy from (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
Add a file entry to the archive with the content copied from the given path, with the given options. The file content is copied usingFiles.copy(Path, OutputStream).- Parameters:
name- the entry name (must not benull)content- the path of the file to copy from (must not benull)options- the entry options; may containZipOptionvalues- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addEntry
Add a file entry to the archive with the content copied from the given path, with the given file attributes. The file content is copied usingFiles.copy(Path, OutputStream).- Parameters:
name- the entry name (must not benull)content- the path of the file to copy from (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closed
-
addEntry
public void addEntry(String name, Path content, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive with the content copied from the given path, with the given options and file attributes. The file content is copied usingFiles.copy(Path, OutputStream).CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)content- the path of the file to copy from (must not benull)options- the entry options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry output stream is still open, or this builder is closedIllegalArgumentException- if conflicting compression options are specifiedUnsupportedOperationException- if an unsupported option is specified
-
addBufferedEntry
Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream, using the builder's default options and no file attributes. The returned file allows random-access writing of the entry data. The entry is always stored with theSTOREDmethod because random-access writing is incompatible with compression. The returned file must be closed before another entry can be added or the archive can be closed. When closed, the CRC-32 is computed from the written data and the local file header is patched with the final CRC and size values.- Parameters:
name- the entry name (must not benull)- Returns:
- the nested file for writing entry data (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closed
-
addBufferedEntry
Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream. The returned file allows random-access writing of the entry data. The entry is always stored with theSTOREDmethod because random-access writing is incompatible with compression. The returned file must be closed before another entry can be added or the archive can be closed. When closed, the CRC-32 is computed from the written data and the local file header is patched with the final CRC and size values.- Parameters:
name- the entry name (must not benull)options- the entry options; may containZipOption.ZIP64- Returns:
- the nested file for writing entry data (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closedIllegalArgumentException- ifZipOption.DEFLATEDis specifiedUnsupportedOperationException- if an unsupported option is specified
-
addBufferedEntry
Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream. The returned file allows random-access writing of the entry data. The entry is always stored with theSTOREDmethod because random-access writing is incompatible with compression. The returned file must be closed before another entry can be added or the archive can be closed. When closed, the CRC-32 is computed from the written data and the local file header is patched with the final CRC and size values.- Parameters:
name- the entry name (must not benull)attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Returns:
- the nested file for writing entry data (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closed
-
addBufferedEntry
public BufferedFile addBufferedEntry(String name, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a file entry to the archive that is backed by a nestedBufferedFilerather than an output stream. The returned file allows random-access writing of the entry data. The entry is always stored with theSTOREDmethod because random-access writing is incompatible with compression. The returned file must be closed before another entry can be added or the archive can be closed. When closed, the CRC-32 is computed from the written data and the local file header is patched with the final CRC and size values.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name (must not benull)options- the entry options (must not benull); may containZipOption.ZIP64attrs- optional file attributes for the entry (e.g., POSIX permissions, timestamps)- Returns:
- the nested file for writing entry data (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closedIllegalArgumentException- ifZipOption.DEFLATEDis specifiedUnsupportedOperationException- if an unsupported option is specified
-
addArchive
Add a nested archive as a STORED entry in this archive. This is a convenience method that combinesaddBufferedEntry(String)andopen(BufferedFile, OpenOption...)into a single call. The returned archive builder writes to a nested region of the outer archive file. When the returned builder is closed, its central directory is written, then the enclosing entry's CRC-32 and size are computed and patched into the outer local file header.The nested builder must be closed before another entry can be added to the outer builder or the outer builder can be closed.
Usage example
try (ArchiveBuilder outer = ArchiveBuilder.open(path)) { try (ArchiveBuilder inner = outer.addArchive("lib/nested.jar")) { try (OutputStream os = inner.addEntry("hello.txt")) { os.write(data); } } }- Parameters:
name- the entry name for the nested archive (must not benull)- Returns:
- a new archive builder for the nested archive (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closed
-
addArchive
Add a nested archive as a STORED entry in this archive with the given options. This is a convenience method that combinesaddBufferedEntry(String, OpenOption...)andopen(BufferedFile, OpenOption...)into a single call. The returned archive builder writes to a nested region of the outer archive file. When the returned builder is closed, its central directory is written, then the enclosing entry's CRC-32 and size are computed and patched into the outer local file header.The nested builder must be closed before another entry can be added to the outer builder or the outer builder can be closed.
ZipOption.ZIP64applies to the outer entry (reserving ZIP64 space in the outer local file header). The inner builder inherits the same options as its defaults.- Parameters:
name- the entry name for the nested archive (must not benull)options- the options; may containZipOptionvalues- Returns:
- a new archive builder for the nested archive (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closedUnsupportedOperationException- if an unsupported option is specified
-
addArchive
Add a nested archive as a STORED entry in this archive with the given file attributes. This is a convenience method that combinesaddBufferedEntry(String, FileAttribute[])andopen(BufferedFile, OpenOption...)into a single call. The returned archive builder writes to a nested region of the outer archive file. When the returned builder is closed, its central directory is written, then the enclosing entry's CRC-32 and size are computed and patched into the outer local file header.The nested builder must be closed before another entry can be added to the outer builder or the outer builder can be closed.
- Parameters:
name- the entry name for the nested archive (must not benull)attrs- optional file attributes for the outer entry (e.g., POSIX permissions, timestamps)- Returns:
- a new archive builder for the nested archive (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closed
-
addArchive
public ArchiveBuilder addArchive(String name, Collection<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Add a nested archive as a STORED entry in this archive with the given options and file attributes. This is a convenience method that combinesaddBufferedEntry(String, Collection, FileAttribute[])andopen(BufferedFile, OpenOption...)into a single call. The returned archive builder writes to a nested region of the outer archive file. When the returned builder is closed, its central directory is written, then the enclosing entry's CRC-32 and size are computed and patched into the outer local file header.The nested builder must be closed before another entry can be added to the outer builder or the outer builder can be closed.
ZipOption.ZIP64applies to the outer entry (reserving ZIP64 space in the outer local file header). The inner builder inherits the same options as its defaults.ZipOption.DEFLATEDis permitted here (unlikeaddBufferedEntry) and sets the inner builder's default compression method; the outer entry is always STORED.CREATE,CREATE_NEW,READ,WRITE, andTRUNCATE_EXISTINGare accepted but ignored, since entries are always created within the archive.- Parameters:
name- the entry name for the nested archive (must not benull)options- the options (must not benull); may containZipOptionvaluesattrs- optional file attributes for the outer entry (e.g., POSIX permissions, timestamps)- Returns:
- a new archive builder for the nested archive (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written, or this builder is closedUnsupportedOperationException- if an unsupported option is specified
-
addRawData
Begin writing raw (non-entry) data to the archive file and return anOutputStreamfor the data. The data is written directly to the underlying file with no local file header, compression, or central directory entry. This is intended for bootstrappers or other tools that hard-code file offsets (obtained fromposition()) to access opaque binary blobs embedded in the archive.The returned stream must be closed before another entry or raw data section can be added, or the archive can be closed.
- Returns:
- an output stream to which the raw data should be written (not
null) - Throws:
IOException- if an I/O error occursIllegalStateException- if an entry or raw data stream is still open, or this builder is closed
-
addRawData
Write raw (non-entry) data to the archive file, reading all bytes from the given input stream. All bytes are read usingInputStream.transferTo(OutputStream). The input stream is not closed by this method.The data is written directly to the underlying file with no local file header, compression, or central directory entry. This is intended for bootstrappers or other tools that hard-code file offsets (obtained from
position()) to access opaque binary blobs embedded in the archive.- Parameters:
content- the input stream to read from (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry or raw data stream is still open, or this builder is closed
-
addRawData
Write raw (non-entry) data to the archive file from the given byte array.The data is written directly to the underlying file with no local file header, compression, or central directory entry. This is intended for bootstrappers or other tools that hard-code file offsets (obtained from
position()) to access opaque binary blobs embedded in the archive.- Parameters:
data- the byte array to write (must not benull)- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry or raw data stream is still open, or this builder is closed
-
addRawData
Write raw (non-entry) data to the archive file from a region of the given byte array.The data is written directly to the underlying file with no local file header, compression, or central directory entry. This is intended for bootstrappers or other tools that hard-code file offsets (obtained from
position()) to access opaque binary blobs embedded in the archive.- Parameters:
data- the byte array to write from (must not benull)offset- the start offset in the arraylength- the number of bytes to write- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry or raw data stream is still open, or this builder is closedIndexOutOfBoundsException- ifoffsetorlengthis negative, oroffset + lengthexceeds the array length
-
close
Close this archive builder, writing the central directory and end-of-central-directory records.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occursIllegalStateException- if an entry is still being written
-