An entry can be written by creating an instance of CpioArchiveEntry and fill it with the necessary values and put it into the CPIO stream. Afterwards write the contents of the file into the CPIO stream. Either close the stream by calling finish() or put a next entry into the cpio stream.
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 final int | |
| pack-priv final String | |
| private boolean | |
| private long | |
| private CpioArchiveEntry | |
| private final short | entryFormat
See |
| private boolean | finished
Indicates if this archive is finished |
| private final HashMap | |
| private long | |
| private final OutputStream | |
| private long | |
| private final ZipEncoding | zipEncoding
The encoding to use for file names and labels. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | CpioArchiveOutputStream(final OutputStream
The cpio stream out, final short The format of the stream format)Constructs the cpio output stream with a specified format, a blocksize of |
| public | CpioArchiveOutputStream(final OutputStream
The cpio stream out, final short The format of the stream format, final int The block size of the archive. blockSize)Constructs the cpio output stream with a specified format using ASCII as the file name encoding. |
| public | CpioArchiveOutputStream(final OutputStream
The cpio stream out, final short The format of the stream format, final int The block size of the archive. blockSize, final String The encoding of file names to write - use null for the platform's default. encoding)Constructs the cpio output stream with a specified format using ASCII as the file name encoding. |
| public | CpioArchiveOutputStream(final OutputStream
The cpio stream out, final String The encoding of file names to write - use null for the platform's default. encoding)Constructs the cpio output stream. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. |
| public void | closeArchiveEntry()
Implements abstract org. |
| public CpioArchiveEntry | createArchiveEntry(final File
the file to create the entry from inputFile, final String name to use for the entry entryName)Implements abstract org. |
| public CpioArchiveEntry | createArchiveEntry(final Path
the file to create the entry from inputPath, final String name to use for the entry entryName, final LinkOption... options indicating how symbolic links are handled. options)Overrides org. |
| private byte[] | |
| private void | |
| public void | finish()
Implements abstract org. |
| private void | |
| public void | putArchiveEntry(final CpioArchiveEntry
the CPIO cpioEntry to be written entry)Implements abstract org. |
| public void | write(final byte[]
the data to be written b, final int the start offset in the data off, final int the number of bytes that are written len)Overrides java. |
| private void | |
| private void | |
| private void | writeCString(final byte[]
the String to write str)Writes an encoded string to the stream followed by \0 |
| private void | |
| private void | |
| private void | |
| private void |
| blockSize | back to summary |
|---|---|
| private final int blockSize | |
| charsetName | back to summary |
|---|---|
| pack-priv final String charsetName | |
| closed | back to summary |
|---|---|
| private boolean closed | |
| crc | back to summary |
|---|---|
| private long crc | |
| entry | back to summary |
|---|---|
| private CpioArchiveEntry entry | |
| entryFormat | back to summary |
|---|---|
| private final short entryFormat See | |
| finished | back to summary |
|---|---|
| private boolean finished Indicates if this archive is finished | |
| names | back to summary |
|---|---|
| private final HashMap<String, CpioArchiveEntry> names | |
| nextArtificalDeviceAndInode | back to summary |
|---|---|
| private long nextArtificalDeviceAndInode | |
| out | back to summary |
|---|---|
| private final OutputStream out | |
| written | back to summary |
|---|---|
| private long written | |
| zipEncoding | back to summary |
|---|---|
| private final ZipEncoding zipEncoding The encoding to use for file names and labels. | |
| CpioArchiveOutputStream | back to summary |
|---|---|
| public CpioArchiveOutputStream(final OutputStream out) Constructs the cpio output stream. The format for this CPIO stream is the "new" format using ASCII encoding for file names
| |
| CpioArchiveOutputStream | back to summary |
|---|---|
| public CpioArchiveOutputStream(final OutputStream out, final short format) Constructs the cpio output stream with a specified format, a blocksize of
| |
| CpioArchiveOutputStream | back to summary |
|---|---|
| public CpioArchiveOutputStream(final OutputStream out, final short format, final int blockSize) Constructs the cpio output stream with a specified format using ASCII as the file name encoding.
| |
| CpioArchiveOutputStream | back to summary |
|---|---|
| public CpioArchiveOutputStream(final OutputStream out, final short format, final int blockSize, final String encoding) Constructs the cpio output stream with a specified format using ASCII as the file name encoding.
| |
| CpioArchiveOutputStream | back to summary |
|---|---|
| public CpioArchiveOutputStream(final OutputStream out, final String encoding) Constructs the cpio output stream. The format for this CPIO stream is the "new" format.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Closes the CPIO output stream as well as the stream being filtered.
| |
| closeArchiveEntry | back to summary |
|---|---|
| public void closeArchiveEntry() throws IOException Implements abstract org. Doc from org. Closes the archive entry, writing any trailer information that may be required.
| |
| encode | back to summary |
|---|---|
| private byte[] encode(final String str) throws IOException Encodes the given string using the configured encoding.
| |
| ensureOpen | back to summary |
|---|---|
| private void ensureOpen() throws IOException Check to make sure that this stream has not been closed
| |
| finish | back to summary |
|---|---|
| public void finish() throws IOException Implements abstract org. Finishes writing the contents of the CPIO output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.
| |
| pad | back to summary |
|---|---|
| private void pad(final int count) throws IOException | |
| putArchiveEntry | back to summary |
|---|---|
| public void putArchiveEntry(final CpioArchiveEntry entry) throws IOException Implements abstract org. Begins writing a new CPIO file entry and positions the stream to the start of the entry data. Closes the current entry if still active. The current time will be used if the entry has no set modification time and the default header format will be used if no other format is specified in the entry.
| |
| write | back to summary |
|---|---|
| public void write(final byte[] b, final int off, final int len) throws IOException Overrides java. Writes an array of bytes to the current CPIO entry data. This method will block until all the bytes are written.
| |
| writeAsciiLong | back to summary |
|---|---|
| private void writeAsciiLong(final long number, final int length, final int radix) throws IOException | |
| writeBinaryLong | back to summary |
|---|---|
| private void writeBinaryLong(final long number, final int length, final boolean swapHalfWord) throws IOException | |
| writeCString | back to summary |
|---|---|
| private void writeCString(final byte[] str) throws IOException Writes an encoded string to the stream followed by \0
| |
| writeHeader | back to summary |
|---|---|
| private void writeHeader(final CpioArchiveEntry e) throws IOException | |
| writeNewEntry | back to summary |
|---|---|
| private void writeNewEntry(final CpioArchiveEntry entry) throws IOException | |
| writeOldAsciiEntry | back to summary |
|---|---|
| private void writeOldAsciiEntry(final CpioArchiveEntry entry) throws IOException | |
| writeOldBinaryEntry | back to summary |
|---|---|
| private void writeOldBinaryEntry(final CpioArchiveEntry entry, final boolean swapHalfWord) throws IOException | |