Class SevenZOutputFile
- java.lang.Object
-
- org.apache.commons.compress.archivers.sevenz.SevenZOutputFile
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class SevenZOutputFile extends java.lang.Object implements java.io.CloseableWrites a 7z file.- Since:
- 1.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classSevenZOutputFile.OutputStreamWrapper
-
Field Summary
Fields Modifier and Type Field Description private CountingOutputStream[]additionalCountingStreamsprivate java.util.Map<SevenZArchiveEntry,long[]>additionalSizesprivate java.nio.channels.SeekableByteChannelchannelprivate java.util.zip.CRC32compressedCrc32private java.lang.Iterable<? extends SevenZMethodConfiguration>contentMethodsprivate java.util.zip.CRC32crc32private CountingOutputStreamcurrentOutputStreamprivate longfileBytesWrittenprivate java.util.List<SevenZArchiveEntry>filesprivate booleanfinishedprivate intnumNonEmptyStreams
-
Constructor Summary
Constructors Constructor Description SevenZOutputFile(java.io.File fileName)Opens file to write a 7z archive to.SevenZOutputFile(java.nio.channels.SeekableByteChannel channel)Prepares channel to write a 7z archive to.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the archive, callingfinish()if necessary.voidcloseArchiveEntry()Closes the archive entry.SevenZArchiveEntrycreateArchiveEntry(java.io.File inputFile, java.lang.String entryName)Create an archive entry using the inputFile and entryName provided.SevenZArchiveEntrycreateArchiveEntry(java.nio.file.Path inputPath, java.lang.String entryName, java.nio.file.LinkOption... options)Create an archive entry using the inputPath and entryName provided.voidfinish()Finishes the addition of entries to this archive, without closing it.private java.lang.Iterable<? extends SevenZMethodConfiguration>getContentMethods(SevenZArchiveEntry entry)private java.io.OutputStreamgetCurrentOutputStream()voidputArchiveEntry(ArchiveEntry archiveEntry)Records an archive entry to add.private static <T> java.lang.Iterable<T>reverse(java.lang.Iterable<T> i)voidsetContentCompression(SevenZMethod method)Sets the default compression method to use for entry contents - the default is LZMA2.voidsetContentMethods(java.lang.Iterable<? extends SevenZMethodConfiguration> methods)Sets the default (compression) methods to use for entry contents - the default is LZMA2.private CountingOutputStreamsetupFileOutputStream()voidwrite(byte[] b)Writes a byte array to the current archive entry.voidwrite(byte[] b, int off, int len)Writes part of a byte array to the current archive entry.voidwrite(int b)Writes a byte to the current archive entry.voidwrite(java.io.InputStream inputStream)Writes all of the given input stream to the current archive entry.voidwrite(java.nio.file.Path path, java.nio.file.OpenOption... options)Writes all of the given input stream to the current archive entry.private voidwriteBits(java.io.DataOutput header, java.util.BitSet bits, int length)private voidwriteFileAntiItems(java.io.DataOutput header)private voidwriteFileATimes(java.io.DataOutput header)private voidwriteFileCTimes(java.io.DataOutput header)private voidwriteFileEmptyFiles(java.io.DataOutput header)private voidwriteFileEmptyStreams(java.io.DataOutput header)private voidwriteFileMTimes(java.io.DataOutput header)private voidwriteFileNames(java.io.DataOutput header)private voidwriteFilesInfo(java.io.DataOutput header)private voidwriteFileWindowsAttributes(java.io.DataOutput header)private voidwriteFolder(java.io.DataOutput header, SevenZArchiveEntry entry)private voidwriteHeader(java.io.DataOutput header)private voidwritePackInfo(java.io.DataOutput header)private voidwriteSingleCodec(SevenZMethodConfiguration m, java.io.OutputStream bos)private voidwriteStreamsInfo(java.io.DataOutput header)private voidwriteSubStreamsInfo(java.io.DataOutput header)private voidwriteUint64(java.io.DataOutput header, long value)private voidwriteUnpackInfo(java.io.DataOutput header)
-
-
-
Field Detail
-
channel
private final java.nio.channels.SeekableByteChannel channel
-
files
private final java.util.List<SevenZArchiveEntry> files
-
numNonEmptyStreams
private int numNonEmptyStreams
-
crc32
private final java.util.zip.CRC32 crc32
-
compressedCrc32
private final java.util.zip.CRC32 compressedCrc32
-
fileBytesWritten
private long fileBytesWritten
-
finished
private boolean finished
-
currentOutputStream
private CountingOutputStream currentOutputStream
-
additionalCountingStreams
private CountingOutputStream[] additionalCountingStreams
-
contentMethods
private java.lang.Iterable<? extends SevenZMethodConfiguration> contentMethods
-
additionalSizes
private final java.util.Map<SevenZArchiveEntry,long[]> additionalSizes
-
-
Constructor Detail
-
SevenZOutputFile
public SevenZOutputFile(java.io.File fileName) throws java.io.IOExceptionOpens file to write a 7z archive to.- Parameters:
fileName- the file to write to- Throws:
java.io.IOException- if opening the file fails
-
SevenZOutputFile
public SevenZOutputFile(java.nio.channels.SeekableByteChannel channel) throws java.io.IOExceptionPrepares channel to write a 7z archive to.SeekableInMemoryByteChannelallows you to write to an in-memory archive.- Parameters:
channel- the channel to write to- Throws:
java.io.IOException- if the channel cannot be positioned properly- Since:
- 1.13
-
-
Method Detail
-
setContentCompression
public void setContentCompression(SevenZMethod method)
Sets the default compression method to use for entry contents - the default is LZMA2.Currently only
SevenZMethod.COPY,SevenZMethod.LZMA2,SevenZMethod.BZIP2andSevenZMethod.DEFLATEare supported.This is a short form for passing a single-element iterable to
setContentMethods(java.lang.Iterable<? extends org.apache.commons.compress.archivers.sevenz.SevenZMethodConfiguration>).- Parameters:
method- the default compression method
-
setContentMethods
public void setContentMethods(java.lang.Iterable<? extends SevenZMethodConfiguration> methods)
Sets the default (compression) methods to use for entry contents - the default is LZMA2.Currently only
SevenZMethod.COPY,SevenZMethod.LZMA2,SevenZMethod.BZIP2andSevenZMethod.DEFLATEare supported.The methods will be consulted in iteration order to create the final output.
- Parameters:
methods- the default (compression) methods- Since:
- 1.8
-
close
public void close() throws java.io.IOExceptionCloses the archive, callingfinish()if necessary.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException- on error
-
createArchiveEntry
public SevenZArchiveEntry createArchiveEntry(java.io.File inputFile, java.lang.String entryName) throws java.io.IOException
Create an archive entry using the inputFile and entryName provided.- Parameters:
inputFile- file to create an entry fromentryName- the name to use- Returns:
- the ArchiveEntry set up with details from the file
- Throws:
java.io.IOException- on error
-
createArchiveEntry
public SevenZArchiveEntry createArchiveEntry(java.nio.file.Path inputPath, java.lang.String entryName, java.nio.file.LinkOption... options) throws java.io.IOException
Create an archive entry using the inputPath and entryName provided.- Parameters:
inputPath- path to create an entry fromentryName- the name to useoptions- options indicating how symbolic links are handled.- Returns:
- the ArchiveEntry set up with details from the file
- Throws:
java.io.IOException- on error- Since:
- 1.21
-
putArchiveEntry
public void putArchiveEntry(ArchiveEntry archiveEntry) throws java.io.IOException
Records an archive entry to add. The caller must then write the content to the archive and callcloseArchiveEntry()to complete the process.- Parameters:
archiveEntry- describes the entry- Throws:
java.io.IOException- on error
-
closeArchiveEntry
public void closeArchiveEntry() throws java.io.IOExceptionCloses the archive entry.- Throws:
java.io.IOException- on error
-
write
public void write(int b) throws java.io.IOExceptionWrites a byte to the current archive entry.- Parameters:
b- The byte to be written.- Throws:
java.io.IOException- on error
-
write
public void write(byte[] b) throws java.io.IOExceptionWrites a byte array to the current archive entry.- Parameters:
b- The byte array to be written.- Throws:
java.io.IOException- on error
-
write
public void write(byte[] b, int off, int len) throws java.io.IOExceptionWrites part of a byte array to the current archive entry.- Parameters:
b- The byte array to be written.off- offset into the array to start writing fromlen- number of bytes to write- Throws:
java.io.IOException- on error
-
write
public void write(java.io.InputStream inputStream) throws java.io.IOExceptionWrites all of the given input stream to the current archive entry.- Parameters:
inputStream- the data source.- Throws:
java.io.IOException- if an I/O error occurs.- Since:
- 1.21
-
write
public void write(java.nio.file.Path path, java.nio.file.OpenOption... options) throws java.io.IOExceptionWrites all of the given input stream to the current archive entry.- Parameters:
path- the data source.options- options specifying how the file is opened.- Throws:
java.io.IOException- if an I/O error occurs.- Since:
- 1.21
-
finish
public void finish() throws java.io.IOExceptionFinishes the addition of entries to this archive, without closing it.- Throws:
java.io.IOException- if archive is already closed.
-
getCurrentOutputStream
private java.io.OutputStream getCurrentOutputStream() throws java.io.IOException- Throws:
java.io.IOException
-
setupFileOutputStream
private CountingOutputStream setupFileOutputStream() throws java.io.IOException
- Throws:
java.io.IOException
-
getContentMethods
private java.lang.Iterable<? extends SevenZMethodConfiguration> getContentMethods(SevenZArchiveEntry entry)
-
writeHeader
private void writeHeader(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeStreamsInfo
private void writeStreamsInfo(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writePackInfo
private void writePackInfo(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeUnpackInfo
private void writeUnpackInfo(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFolder
private void writeFolder(java.io.DataOutput header, SevenZArchiveEntry entry) throws java.io.IOException- Throws:
java.io.IOException
-
writeSingleCodec
private void writeSingleCodec(SevenZMethodConfiguration m, java.io.OutputStream bos) throws java.io.IOException
- Throws:
java.io.IOException
-
writeSubStreamsInfo
private void writeSubStreamsInfo(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFilesInfo
private void writeFilesInfo(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileEmptyStreams
private void writeFileEmptyStreams(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileEmptyFiles
private void writeFileEmptyFiles(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileAntiItems
private void writeFileAntiItems(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileNames
private void writeFileNames(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileCTimes
private void writeFileCTimes(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileATimes
private void writeFileATimes(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileMTimes
private void writeFileMTimes(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeFileWindowsAttributes
private void writeFileWindowsAttributes(java.io.DataOutput header) throws java.io.IOException- Throws:
java.io.IOException
-
writeUint64
private void writeUint64(java.io.DataOutput header, long value) throws java.io.IOException- Throws:
java.io.IOException
-
writeBits
private void writeBits(java.io.DataOutput header, java.util.BitSet bits, int length) throws java.io.IOException- Throws:
java.io.IOException
-
reverse
private static <T> java.lang.Iterable<T> reverse(java.lang.Iterable<T> i)
-
-