org.apache.commons.compress.archivers.cpio
public class CpioArchiveOutputStream extends ArchiveOutputStream implements CpioConstants
CpioArchiveOutputStream out = new CpioArchiveOutputStream(
new FileOutputStream(new File("test.cpio")));
CpioArchiveEntry entry = new CpioArchiveEntry();
entry.setName("testfile");
String contents = "12345";
entry.setFileSize(contents.length());
entry.setMode(CpioConstants.C_ISREG); // regular file
... set other attributes, e.g. time, number of links
out.putArchiveEntry(entry);
out.write(testContents.getBytes());
out.close();
Note: This implementation should be compatible to cpio 2.5
This class uses mutable fields and is not considered threadsafe.
based on code from the jRPM project (jrpm.sourceforge.net)Modifier and Type | Field and Description |
---|---|
private int |
blockSize |
private boolean |
closed |
private long |
crc |
private CpioArchiveEntry |
entry |
private short |
entryFormat
See
CpioArchiveEntry#setFormat(short) for possible values. |
private boolean |
finished
indicates if this archive is finished
|
private java.util.HashMap |
names |
private long |
nextArtificalDeviceAndInode |
private java.io.OutputStream |
out |
private long |
written |
BLOCK_SIZE, C_IRGRP, C_IROTH, C_IRUSR, C_ISBLK, C_ISCHR, C_ISDIR, C_ISFIFO, C_ISGID, C_ISLNK, C_ISNWK, C_ISREG, C_ISSOCK, C_ISUID, C_ISVTX, C_IWGRP, C_IWOTH, C_IWUSR, C_IXGRP, C_IXOTH, C_IXUSR, CPIO_TRAILER, FORMAT_NEW, FORMAT_NEW_CRC, FORMAT_NEW_MASK, FORMAT_OLD_ASCII, FORMAT_OLD_BINARY, FORMAT_OLD_MASK, MAGIC_NEW, MAGIC_NEW_CRC, MAGIC_OLD_ASCII, MAGIC_OLD_BINARY, S_IFMT
Constructor and Description |
---|
CpioArchiveOutputStream(java.io.OutputStream out)
Construct the cpio output stream.
|
CpioArchiveOutputStream(java.io.OutputStream out,
short format)
Construct the cpio output stream with a specified format and a
blocksize of
BLOCK_SIZE . |
CpioArchiveOutputStream(java.io.OutputStream out,
short format,
int blockSize)
Construct the cpio output stream with a specified format
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the CPIO output stream as well as the stream being filtered.
|
void |
closeArchiveEntry()
Closes the archive entry, writing any trailer information that may
be required.
|
ArchiveEntry |
createArchiveEntry(java.io.File inputFile,
java.lang.String entryName)
Creates a new ArchiveEntry.
|
private void |
ensureOpen()
Check to make sure that this stream has not been closed
|
void |
finish()
Finishes writing the contents of the CPIO output stream without closing
the underlying stream.
|
private void |
pad(int count) |
void |
putArchiveEntry(ArchiveEntry entry)
Begins writing a new CPIO file entry and positions the stream to the
start of the entry data.
|
void |
write(byte[] b,
int off,
int len)
Writes an array of bytes to the current CPIO entry data.
|
private void |
writeAsciiLong(long number,
int length,
int radix) |
private void |
writeBinaryLong(long number,
int length,
boolean swapHalfWord) |
private void |
writeCString(java.lang.String str)
Writes an ASCII string to the stream followed by \0
|
private void |
writeHeader(CpioArchiveEntry e) |
private void |
writeNewEntry(CpioArchiveEntry entry) |
private void |
writeOldAsciiEntry(CpioArchiveEntry entry) |
private void |
writeOldBinaryEntry(CpioArchiveEntry entry,
boolean swapHalfWord) |
canWriteEntryData, count, count, getBytesWritten, getCount, write
private CpioArchiveEntry entry
private boolean closed
private boolean finished
private final short entryFormat
CpioArchiveEntry#setFormat(short)
for possible values.private final java.util.HashMap names
private long crc
private long written
private final java.io.OutputStream out
private final int blockSize
private long nextArtificalDeviceAndInode
public CpioArchiveOutputStream(java.io.OutputStream out, short format)
BLOCK_SIZE
.out
- The cpio streamformat
- The format of the streampublic CpioArchiveOutputStream(java.io.OutputStream out, short format, int blockSize)
out
- The cpio streamformat
- The format of the streamblockSize
- The block size of the archive.public CpioArchiveOutputStream(java.io.OutputStream out)
out
- The cpio streamprivate void ensureOpen() throws java.io.IOException
java.io.IOException
- if the stream is already closedpublic void putArchiveEntry(ArchiveEntry entry) throws java.io.IOException
putArchiveEntry
in class ArchiveOutputStream
entry
- the CPIO cpioEntry to be writtenjava.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredjava.lang.ClassCastException
- if entry is not an instance of CpioArchiveEntryprivate void writeHeader(CpioArchiveEntry e) throws java.io.IOException
java.io.IOException
private void writeNewEntry(CpioArchiveEntry entry) throws java.io.IOException
java.io.IOException
private void writeOldAsciiEntry(CpioArchiveEntry entry) throws java.io.IOException
java.io.IOException
private void writeOldBinaryEntry(CpioArchiveEntry entry, boolean swapHalfWord) throws java.io.IOException
java.io.IOException
public void closeArchiveEntry() throws java.io.IOException
ArchiveOutputStream
closeArchiveEntry
in class ArchiveOutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
b
- the data to be writtenoff
- the start offset in the datalen
- the number of bytes that are writtenjava.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredpublic void finish() throws java.io.IOException
finish
in class ArchiveOutputStream
java.io.IOException
- if an I/O exception has occurred or if a CPIO file error has
occurredpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in class java.io.OutputStream
java.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredprivate void pad(int count) throws java.io.IOException
java.io.IOException
private void writeBinaryLong(long number, int length, boolean swapHalfWord) throws java.io.IOException
java.io.IOException
private void writeAsciiLong(long number, int length, int radix) throws java.io.IOException
java.io.IOException
private void writeCString(java.lang.String str) throws java.io.IOException
str
- the String to writejava.io.IOException
- if the string couldn't be writtenpublic ArchiveEntry createArchiveEntry(java.io.File inputFile, java.lang.String entryName) throws java.io.IOException
createArchiveEntry
in class ArchiveOutputStream
java.io.IOException
ArchiveOutputStream.createArchiveEntry(java.io.File, java.lang.String)