Index: src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java =================================================================== --- src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java (revision 758328) +++ src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java (working copy) @@ -39,14 +39,13 @@ *
*
* CpioArchiveOutputStream out = new CpioArchiveOutputStream(
- * new FileOutputStream(new File("test.cpio")));
+ * new FileOutputStream(new File("test.cpio")));
* CpioArchiveEntry entry = new CpioArchiveEntry();
* entry.setName("testfile");
* String contents = "12345";
* entry.setFileSize(contents.length());
* out.putNextEntry(entry);
* out.write(testContents.getBytes());
- * out.finish();
* out.close();
*
*
@@ -150,7 +149,7 @@
public void putNextEntry(final CpioArchiveEntry e) throws IOException {
ensureOpen();
if (this.cpioEntry != null) {
- closeEntry(); // close previous entry
+ closeArchiveEntry(); // close previous entry
}
if (e.getTime() == -1) {
e.setTime(System.currentTimeMillis());
@@ -243,37 +242,6 @@
}
/**
- * Closes the current CPIO entry and positions the stream for writing the
- * next entry.
- *
- * @throws IOException
- * if an I/O error has occurred or if a CPIO file error has
- * occurred
- */
- public void closeEntry() throws IOException {
- ensureOpen();
-
- if (this.cpioEntry.getSize() != this.written) {
- throw new IOException("invalid entry size (expected "
- + this.cpioEntry.getSize() + " but got " + this.written
- + " bytes)");
- }
- if ((this.cpioEntry.getFormat() | FORMAT_NEW_MASK) == FORMAT_NEW_MASK) {
- pad(this.cpioEntry.getSize(), 4);
- } else if ((this.cpioEntry.getFormat() | FORMAT_OLD_BINARY) == FORMAT_OLD_BINARY) {
- pad(this.cpioEntry.getSize(), 2);
- }
- if ((this.cpioEntry.getFormat() | FORMAT_NEW_CRC) == FORMAT_NEW_CRC) {
- if (this.crc != this.cpioEntry.getChksum()) {
- throw new IOException("CRC Error");
- }
- }
- this.cpioEntry = null;
- this.crc = 0;
- this.written = 0;
- }
-
- /**
* Writes an array of bytes to the current CPIO entry data. This method will
* block until all the bytes are written.
*
@@ -327,14 +295,14 @@
return;
}
if (this.cpioEntry != null) {
- closeEntry();
+ closeArchiveEntry();
}
this.cpioEntry = new CpioArchiveEntry(this.entryFormat);
this.cpioEntry.setMode(0);
this.cpioEntry.setName("TRAILER!!!");
this.cpioEntry.setNumberOfLinks(1);
writeHeader(this.cpioEntry);
- closeEntry();
+ closeArchiveEntry();
}
/**
@@ -345,6 +313,7 @@
* occurred
*/
public void close() throws IOException {
+ this.finish();
if (!this.closed) {
super.close();
this.closed = true;
@@ -394,15 +363,33 @@
out.write('\0');
}
- /*
- * (non-Javadoc)
+ /*(non-Javadoc)
*
* @see
* org.apache.commons.compress.archivers.ArchiveOutputStream#closeArchiveEntry
* ()
*/
public void closeArchiveEntry() throws IOException {
- this.closeEntry();
+ ensureOpen();
+
+ if (this.cpioEntry.getSize() != this.written) {
+ throw new IOException("invalid entry size (expected "
+ + this.cpioEntry.getSize() + " but got " + this.written
+ + " bytes)");
+ }
+ if ((this.cpioEntry.getFormat() | FORMAT_NEW_MASK) == FORMAT_NEW_MASK) {
+ pad(this.cpioEntry.getSize(), 4);
+ } else if ((this.cpioEntry.getFormat() | FORMAT_OLD_BINARY) == FORMAT_OLD_BINARY) {
+ pad(this.cpioEntry.getSize(), 2);
+ }
+ if ((this.cpioEntry.getFormat() | FORMAT_NEW_CRC) == FORMAT_NEW_CRC) {
+ if (this.crc != this.cpioEntry.getChksum()) {
+ throw new IOException("CRC Error");
+ }
+ }
+ this.cpioEntry = null;
+ this.crc = 0;
+ this.written = 0;
}
/*
Index: src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
===================================================================
--- src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java (revision 758328)
+++ src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java (working copy)
@@ -23,6 +23,8 @@
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.commons.compress.AbstractTestCase;
import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
@@ -64,22 +66,35 @@
os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length()));
IOUtils.copy(new FileInputStream(file2), os);
os.closeArchiveEntry();
+
os.close();
+ out.close();
}
-
+
// Unarchive Operation
final File input = output;
final InputStream is = new FileInputStream(input);
final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("cpio", is);
- final CpioArchiveEntry entry = (CpioArchiveEntry)in.getNextEntry();
- File target = new File(dir, entry.getName());
- final OutputStream out = new FileOutputStream(target);
+
+ Map result = new HashMap();
+ ArchiveEntry entry = null;
+ while ((entry = in.getNextEntry()) != null) {
+ File target = new File(dir, entry.getName());
+ final OutputStream out = new FileOutputStream(target);
+ IOUtils.copy(in, out);
+ out.close();
+ result.put(entry.getName(), target);
+ }
+ in.close();
- IOUtils.copy(in, out);
-
- out.close();
- in.close();
+ File t = (File)result.get("test1.xml");
+ assertTrue(t.exists());
+ assertEquals(t.length(), 76);
+
+ t = (File)result.get("test2.xml");
+ assertTrue(t.exists());
+ assertEquals(t.length(), 78);
}
}
Index: src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
===================================================================
--- src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java (revision 0)
+++ src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java (revision 0)
@@ -0,0 +1,31 @@
+package org.apache.commons.compress.archivers.cpio;
+
+import java.io.FileInputStream;
+
+import org.apache.commons.compress.AbstractTestCase;
+
+public class CpioArchiveInputStreamTest extends AbstractTestCase {
+
+ public void testCpioUnarchive() throws Exception {
+ StringBuffer expected = new StringBuffer();
+ expected.append("./test1.xml\n");
+ expected.append("