You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/08/08 20:39:50 UTC
[commons-compress] branch master updated: - Add
ArArchiveOutputStream.createArchiveEntry(Path, String,
LinkOption...). - Add ArArchiveEntry(Path, String,
LinkOption...). - Fix formatting in method signatures. - No need to nest an
else clause.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new 9cd1221 - Add ArArchiveOutputStream.createArchiveEntry(Path, String, LinkOption...). - Add ArArchiveEntry(Path, String, LinkOption...). - Fix formatting in method signatures. - No need to nest an else clause.
9cd1221 is described below
commit 9cd1221cc7eb094eedd44d7a311852ec963b7e1a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Aug 8 16:36:49 2020 -0400
- Add ArArchiveOutputStream.createArchiveEntry(Path, String,
LinkOption...).
- Add ArArchiveEntry(Path, String, LinkOption...).
- Fix formatting in method signatures.
- No need to nest an else clause.
---
src/changes/changes.xml | 6 ++
.../compress/archivers/ar/ArArchiveEntry.java | 22 ++++++-
.../archivers/ar/ArArchiveOutputStream.java | 29 ++++++---
.../commons/compress/archivers/ArTestCase.java | 70 +++++++++++++++++++---
4 files changed, 108 insertions(+), 19 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 777b9d6..f09a45f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -140,6 +140,12 @@ The <action> type attribute can be add,update,fix,remove.
Add SevenZOutputFile.createArchiveEntry(Path, String).
</action>
<action type="update" date="2020-08-08" due-to="Gary Gregory" dev="ggregory">
+ Add ArArchiveOutputStream.createArchiveEntry(Path, String, LinkOption...).
+ </action>
+ <action type="update" date="2020-08-08" due-to="Gary Gregory" dev="ggregory">
+ Add ArArchiveEntry(Path, String, LinkOption...)
+ </action>
+ <action type="update" date="2020-08-08" due-to="Gary Gregory" dev="ggregory">
Add Path support to ZipArchiveOutputStream #123.
</action>
<action type="update" date="2020-07-23" due-to="Dependabot" dev="ggregory">
diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveEntry.java
index df9595a..863a2de 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveEntry.java
@@ -19,6 +19,10 @@
package org.apache.commons.compress.archivers.ar;
import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
import java.util.Date;
import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -107,7 +111,7 @@ public class ArArchiveEntry implements ArchiveEntry {
}
/**
- * Create a new instance using the attributes of the given file
+ * Creates a new instance using the attributes of the given file
* @param inputFile the file to create an entry from
* @param entryName the name of the entry
*/
@@ -117,6 +121,19 @@ public class ArArchiveEntry implements ArchiveEntry {
0, 0, DEFAULT_MODE, inputFile.lastModified() / 1000);
}
+ /**
+ * Creates a new instance using the attributes of the given file
+ * @param inputPath the file to create an entry from
+ * @param entryName the name of the entry
+ * @param options options indicating how symbolic links are handled.
+ * @throws IOException if an I/O error occurs.
+ * @since 1.21
+ */
+ public ArArchiveEntry(Path inputPath, String entryName, LinkOption... options) throws IOException {
+ this(entryName, Files.isRegularFile(inputPath, options) ? Files.size(inputPath) : 0, 0, 0, DEFAULT_MODE,
+ Files.getLastModifiedTime(inputPath, options).toMillis() / 1000);
+ }
+
@Override
public long getSize() {
return this.getLength();
@@ -180,8 +197,7 @@ public class ArArchiveEntry implements ArchiveEntry {
final ArArchiveEntry other = (ArArchiveEntry) obj;
if (name == null) {
return other.name == null;
- } else {
- return name.equals(other.name);
}
+ return name.equals(other.name);
}
}
diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
index 59aa07f..5ef781f 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
@@ -22,6 +22,8 @@ import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
@@ -48,7 +50,7 @@ public class ArArchiveOutputStream extends ArchiveOutputStream {
/** indicates if this archive is finished */
private boolean finished = false;
- public ArArchiveOutputStream( final OutputStream pOut ) {
+ public ArArchiveOutputStream(final OutputStream pOut) {
this.out = pOut;
}
@@ -85,7 +87,7 @@ public class ArArchiveOutputStream extends ArchiveOutputStream {
}
@Override
- public void putArchiveEntry( final ArchiveEntry pEntry ) throws IOException {
+ public void putArchiveEntry(final ArchiveEntry pEntry) throws IOException {
if(finished) {
throw new IOException("Stream has already been finished");
}
@@ -111,7 +113,7 @@ public class ArArchiveOutputStream extends ArchiveOutputStream {
haveUnclosedEntry = true;
}
- private long fill( final long pOffset, final long pNewOffset, final char pFill ) throws IOException {
+ private long fill(final long pOffset, final long pNewOffset, final char pFill) throws IOException {
final long diff = pNewOffset - pOffset;
if (diff > 0) {
@@ -123,13 +125,13 @@ public class ArArchiveOutputStream extends ArchiveOutputStream {
return pNewOffset;
}
- private long write( final String data ) throws IOException {
+ private long write(final String data) throws IOException {
final byte[] bytes = data.getBytes(StandardCharsets.US_ASCII);
write(bytes);
return bytes.length;
}
- private long writeEntryHeader( final ArArchiveEntry pEntry ) throws IOException {
+ private long writeEntryHeader(final ArArchiveEntry pEntry) throws IOException {
long offset = 0;
boolean mustAppendName = false;
@@ -219,12 +221,25 @@ public class ArArchiveOutputStream extends ArchiveOutputStream {
@Override
public ArchiveEntry createArchiveEntry(final File inputFile, final String entryName)
- throws IOException {
- if(finished) {
+ throws IOException {
+ if (finished) {
throw new IOException("Stream has already been finished");
}
return new ArArchiveEntry(inputFile, entryName);
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * @since 1.21
+ */
+ @Override
+ public ArchiveEntry createArchiveEntry(Path inputPath, String entryName, LinkOption... options) throws IOException {
+ if (finished) {
+ throw new IOException("Stream has already been finished");
+ }
+ return new ArArchiveEntry(inputPath, entryName, options);
+ }
@Override
public void finish() throws IOException {
diff --git a/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java b/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
index 7174ca5..47ccba9 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
@@ -277,14 +277,16 @@ public final class ArTestCase extends AbstractTestCase {
ArArchiveOutputStream aos = null;
ArArchiveInputStream ais = null;
FileInputStream fis = null;
+ final File directory = tmp[0];
+ final File file = tmp[1];
try {
- archive = File.createTempFile("test.", ".ar", tmp[0]);
+ archive = File.createTempFile("test.", ".ar", directory);
archive.deleteOnExit();
aos = new ArArchiveOutputStream(new FileOutputStream(archive));
- final ArArchiveEntry in = new ArArchiveEntry(tmp[1], "foo");
+ final ArArchiveEntry in = new ArArchiveEntry(file, "foo");
aos.putArchiveEntry(in);
- final byte[] b = new byte[(int) tmp[1].length()];
- fis = new FileInputStream(tmp[1]);
+ final byte[] b = new byte[(int) file.length()];
+ fis = new FileInputStream(file);
while (fis.read(b) > 0) {
aos.write(b);
}
@@ -299,10 +301,9 @@ public final class ArTestCase extends AbstractTestCase {
ais = null;
assertNotNull(out);
assertEquals("foo", out.getName());
- assertEquals(tmp[1].length(), out.getSize());
+ assertEquals(file.length(), out.getSize());
// AR stores time with a granularity of 1 second
- assertEquals(tmp[1].lastModified() / 1000,
- out.getLastModifiedDate().getTime() / 1000);
+ assertEquals(file.lastModified() / 1000, out.getLastModifiedDate().getTime() / 1000);
assertFalse(out.isDirectory());
} finally {
if (ais != null) {
@@ -315,8 +316,59 @@ public final class ArTestCase extends AbstractTestCase {
if (fis != null) {
fis.close();
}
- tryHardToDelete(tmp[1]);
- rmdir(tmp[0]);
+ tryHardToDelete(file);
+ rmdir(directory);
+ }
+ }
+
+ @Test
+ public void testFileEntryFromPath() throws Exception {
+ final File[] tmp = createTempDirAndFile();
+ File archive = null;
+ ArArchiveOutputStream aos = null;
+ ArArchiveInputStream ais = null;
+ FileInputStream fis = null;
+ final File directory = tmp[0];
+ final File file = tmp[1];
+ try {
+ archive = File.createTempFile("test.", ".ar", directory);
+ archive.deleteOnExit();
+ aos = new ArArchiveOutputStream(new FileOutputStream(archive));
+ final ArArchiveEntry in = new ArArchiveEntry(file.toPath(), "foo");
+ aos.putArchiveEntry(in);
+ final byte[] b = new byte[(int) file.length()];
+ fis = new FileInputStream(file);
+ while (fis.read(b) > 0) {
+ aos.write(b);
+ }
+ fis.close();
+ fis = null;
+ aos.closeArchiveEntry();
+ aos.close();
+ aos = null;
+ ais = new ArArchiveInputStream(new FileInputStream(archive));
+ final ArArchiveEntry out = ais.getNextArEntry();
+ ais.close();
+ ais = null;
+ assertNotNull(out);
+ assertEquals("foo", out.getName());
+ assertEquals(file.length(), out.getSize());
+ // AR stores time with a granularity of 1 second
+ assertEquals(file.lastModified() / 1000, out.getLastModifiedDate().getTime() / 1000);
+ assertFalse(out.isDirectory());
+ } finally {
+ if (ais != null) {
+ ais.close();
+ }
+ if (aos != null) {
+ aos.close();
+ }
+ tryHardToDelete(archive);
+ if (fis != null) {
+ fis.close();
+ }
+ tryHardToDelete(file);
+ rmdir(directory);
}
}