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.Closeable
Writes a 7z file.- Since:
- 1.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
SevenZOutputFile.OutputStreamWrapper
-
Field Summary
Fields Modifier and Type Field Description private CountingOutputStream[]
additionalCountingStreams
private java.util.Map<SevenZArchiveEntry,long[]>
additionalSizes
private java.nio.channels.SeekableByteChannel
channel
private java.util.zip.CRC32
compressedCrc32
private java.lang.Iterable<? extends SevenZMethodConfiguration>
contentMethods
private java.util.zip.CRC32
crc32
private CountingOutputStream
currentOutputStream
private long
fileBytesWritten
private java.util.List<SevenZArchiveEntry>
files
private boolean
finished
private int
numNonEmptyStreams
-
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 void
close()
Closes the archive, callingfinish()
if necessary.void
closeArchiveEntry()
Closes the archive entry.SevenZArchiveEntry
createArchiveEntry(java.io.File inputFile, java.lang.String entryName)
Create an archive entry using the inputFile and entryName provided.void
finish()
Finishes the addition of entries to this archive, without closing it.private java.lang.Iterable<? extends SevenZMethodConfiguration>
getContentMethods(SevenZArchiveEntry entry)
private java.io.OutputStream
getCurrentOutputStream()
void
putArchiveEntry(ArchiveEntry archiveEntry)
Records an archive entry to add.private static <T> java.lang.Iterable<T>
reverse(java.lang.Iterable<T> i)
void
setContentCompression(SevenZMethod method)
Sets the default compression method to use for entry contents - the default is LZMA2.void
setContentMethods(java.lang.Iterable<? extends SevenZMethodConfiguration> methods)
Sets the default (compression) methods to use for entry contents - the default is LZMA2.private CountingOutputStream
setupFileOutputStream()
void
write(byte[] b)
Writes a byte array to the current archive entry.void
write(byte[] b, int off, int len)
Writes part of a byte array to the current archive entry.void
write(int b)
Writes a byte to the current archive entry.private void
writeBits(java.io.DataOutput header, java.util.BitSet bits, int length)
private void
writeFileAntiItems(java.io.DataOutput header)
private void
writeFileATimes(java.io.DataOutput header)
private void
writeFileCTimes(java.io.DataOutput header)
private void
writeFileEmptyFiles(java.io.DataOutput header)
private void
writeFileEmptyStreams(java.io.DataOutput header)
private void
writeFileMTimes(java.io.DataOutput header)
private void
writeFileNames(java.io.DataOutput header)
private void
writeFilesInfo(java.io.DataOutput header)
private void
writeFileWindowsAttributes(java.io.DataOutput header)
private void
writeFolder(java.io.DataOutput header, SevenZArchiveEntry entry)
private void
writeHeader(java.io.DataOutput header)
private void
writePackInfo(java.io.DataOutput header)
private void
writeSingleCodec(SevenZMethodConfiguration m, java.io.OutputStream bos)
private void
writeStreamsInfo(java.io.DataOutput header)
private void
writeSubStreamsInfo(java.io.DataOutput header)
private void
writeUint64(java.io.DataOutput header, long value)
private void
writeUnpackInfo(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.IOException
Opens 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.IOException
Prepares channel to write a 7z archive to.SeekableInMemoryByteChannel
allows 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.BZIP2
andSevenZMethod.DEFLATE
are 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.BZIP2
andSevenZMethod.DEFLATE
are 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.IOException
Closes the archive, callingfinish()
if necessary.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in 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
-
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.IOException
Closes the archive entry.- Throws:
java.io.IOException
- on error
-
write
public void write(int b) throws java.io.IOException
Writes 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.IOException
Writes 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.IOException
Writes 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
-
finish
public void finish() throws java.io.IOException
Finishes 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)
-
-