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 2024/02/10 20:59:23 UTC
(commons-compress) branch master updated: New package-private constructors can be private
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 a3f5eb616 New package-private constructors can be private
a3f5eb616 is described below
commit a3f5eb61657b086728d3a4e981f2c9d0b11112b2
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Feb 10 15:59:18 2024 -0500
New package-private constructors can be private
Internal refactorings
---
.../compress/archivers/zip/ZipArchiveEntry.java | 190 +++++++++++----------
1 file changed, 104 insertions(+), 86 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
index 3e0d96924..6d3d41d97 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
@@ -217,6 +217,8 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
UNICODE_EXTRA_FIELD
}
+ private static final String ZIP_DIR_SEP = "/";
+
static final ZipArchiveEntry[] EMPTY_ARRAY = {};
static LinkedList<ZipArchiveEntry> EMPTY_LINKED_LIST = new LinkedList<>();
@@ -232,19 +234,35 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
return TimeUtils.isUnixTime(lastModifiedTime) && TimeUtils.isUnixTime(lastAccessTime) && TimeUtils.isUnixTime(creationTime);
}
+ private static boolean isDirectoryEntryName(final String entryName) {
+ return entryName.endsWith(ZIP_DIR_SEP);
+ }
+
+ private static String toDirectoryEntryName(final String entryName) {
+ return isDirectoryEntryName(entryName) ? entryName : entryName + ZIP_DIR_SEP;
+ }
+
+ private static String toEntryName(final File inputFile, final String entryName) {
+ return inputFile.isDirectory() ? toDirectoryEntryName(entryName) : entryName;
+ }
+
+ private static String toEntryName(final Path inputPath, final String entryName, final LinkOption... options) {
+ return Files.isDirectory(inputPath, options) ? toDirectoryEntryName(entryName) : entryName;
+ }
+
/**
* The {@link java.util.zip.ZipEntry} base class only supports the compression methods STORED and DEFLATED. We override the field so that any compression
* methods can be used.
* <p>
* The default value -1 means that the method has not been specified.
- *
+ * </p>
* @see <a href="https://issues.apache.org/jira/browse/COMPRESS-93" >COMPRESS-93</a>
*/
private int method = ZipMethod.UNKNOWN_CODE;
+
/**
* The {@link java.util.zip.ZipEntry#setSize} method in the base class throws an IllegalArgumentException if the size is bigger than 2GB for Java versions
* < 7 and even in Java 7+ if the implementation in java.util.zip doesn't support Zip64 itself (it is an optional feature).
- *
* <p>
* We need to keep our own size information for Zip64 support.
* </p>
@@ -265,7 +283,9 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
private long localHeaderOffset = OFFSET_UNKNOWN;
private long dataOffset = OFFSET_UNKNOWN;
private boolean isStreamContiguous;
+
private NameSource nameSource = NameSource.NAME;
+
private final Function<ZipShort, ZipExtraField> extraFieldFactory;
private CommentSource commentSource = CommentSource.COMMENT;
@@ -290,20 +310,11 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
* will be stripped from the entry name.
* </p>
*
- * @param extraFieldFactory custom lookup factory for extra fields or null
* @param inputFile file to create the entry from
* @param entryName name of the entry
*/
- ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory, final File inputFile, final String entryName) {
- this(extraFieldFactory, inputFile.isDirectory() && !entryName.endsWith("/") ? entryName + "/" : entryName);
- try {
- setAttributes(inputFile.toPath());
- } catch (final IOException e) { // NOSONAR
- if (inputFile.isFile()) {
- setSize(inputFile.length());
- }
- setTime(inputFile.lastModified());
- }
+ public ZipArchiveEntry(final File inputFile, final String entryName) {
+ this(null, inputFile, entryName);
}
/**
@@ -314,25 +325,20 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
* will be stripped from the entry name.
* </p>
*
+ * @param extraFieldFactory custom lookup factory for extra fields or null
* @param inputFile file to create the entry from
* @param entryName name of the entry
*/
- public ZipArchiveEntry(final File inputFile, final String entryName) {
- this(null, inputFile, entryName);
- }
-
- /**
- * Creates a new ZIP entry with fields taken from the specified ZIP entry.
- *
- * <p>
- * Assumes the entry represents a directory if and only if the name ends with a forward slash "/".
- * </p>
- *
- * @param entry the entry to get fields from
- * @throws ZipException on error
- */
- public ZipArchiveEntry(final java.util.zip.ZipEntry entry) throws ZipException {
- this(null, entry);
+ private ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory, final File inputFile, final String entryName) {
+ this(extraFieldFactory, toEntryName(inputFile, entryName));
+ try {
+ setAttributes(inputFile.toPath());
+ } catch (final IOException e) { // NOSONAR
+ if (inputFile.isFile()) {
+ setSize(inputFile.length());
+ }
+ setTime(inputFile.lastModified());
+ }
}
/**
@@ -346,8 +352,7 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
* @param entry the entry to get fields from
* @throws ZipException on error
*/
- ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory,
- final java.util.zip.ZipEntry entry) throws ZipException {
+ private ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory, final java.util.zip.ZipEntry entry) throws ZipException {
super(entry);
this.extraFieldFactory = extraFieldFactory;
setName(entry.getName());
@@ -370,48 +375,64 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
* will be stripped from the entry name.
* </p>
*
+ * @param extraFieldFactory custom lookup factory for extra fields or null
* @param inputPath path to create the entry from.
* @param entryName 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 ZipArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException {
- this(null, inputPath, entryName, options);
+ private ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory, final Path inputPath, final String entryName,
+ final LinkOption... options) throws IOException {
+ this(extraFieldFactory, toEntryName(inputPath, entryName, options));
+ setAttributes(inputPath, options);
}
/**
- * Creates a new ZIP entry taking some information from the given path and using the provided name.
+ * Creates a new ZIP entry with the specified name.
*
* <p>
- * The name will be adjusted to end with a forward slash "/" if the file is a directory. If the file is not a directory a potential trailing forward slash
- * will be stripped from the entry name.
+ * Assumes the entry represents a directory if and only if the name ends with a forward slash "/".
* </p>
*
* @param extraFieldFactory custom lookup factory for extra fields or null
- * @param inputPath path to create the entry from.
- * @param entryName name of the entry.
- * @param options options indicating how symbolic links are handled.
- * @throws IOException if an I/O error occurs.
+ * @param name the name of the entry
*/
- ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory,
- final Path inputPath, final String entryName, final LinkOption... options) throws IOException {
- this(extraFieldFactory, Files.isDirectory(inputPath, options) && !entryName.endsWith("/") ? entryName + "/" : entryName);
- setAttributes(inputPath, options);
+ private ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory, final String name) {
+ super(name);
+ this.extraFieldFactory = extraFieldFactory;
+ setName(name);
}
/**
- * Creates a new ZIP entry with the specified name.
+ * Creates a new ZIP entry with fields taken from the specified ZIP entry.
*
* <p>
* Assumes the entry represents a directory if and only if the name ends with a forward slash "/".
* </p>
*
- * @param name the name of the entry
- * @since 1.26.0
+ * @param entry the entry to get fields from
+ * @throws ZipException on error
*/
- public ZipArchiveEntry(final String name) {
- this((Function<ZipShort, ZipExtraField>) null, name);
+ public ZipArchiveEntry(final java.util.zip.ZipEntry entry) throws ZipException {
+ this(null, entry);
+ }
+
+ /**
+ * Creates a new ZIP entry taking some information from the given path and using the provided name.
+ *
+ * <p>
+ * The name will be adjusted to end with a forward slash "/" if the file is a directory. If the file is not a directory a potential trailing forward slash
+ * will be stripped from the entry name.
+ * </p>
+ *
+ * @param inputPath path to create the entry from.
+ * @param entryName 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 ZipArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException {
+ this(null, inputPath, entryName, options);
}
/**
@@ -421,14 +442,11 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
* Assumes the entry represents a directory if and only if the name ends with a forward slash "/".
* </p>
*
- * @param extraFieldFactory custom lookup factory for extra fields or null
* @param name the name of the entry
+ * @since 1.26.0
*/
- ZipArchiveEntry(final Function<ZipShort, ZipExtraField> extraFieldFactory,
- final String name) {
- super(name);
- this.extraFieldFactory = extraFieldFactory;
- setName(name);
+ public ZipArchiveEntry(final String name) {
+ this((Function<ZipShort, ZipExtraField>) null, name);
}
/**
@@ -1020,7 +1038,7 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
*/
@Override
public boolean isDirectory() {
- return getName().endsWith("/");
+ return isDirectoryEntryName(getName());
}
@Override
@@ -1086,6 +1104,34 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
}
}
+ private ZipExtraField[] parseExtraFields(final byte[] data, final boolean local,
+ final ExtraFieldParsingBehavior parsingBehavior) throws ZipException {
+ if (extraFieldFactory != null) {
+ return ExtraFieldUtils.parse(data, local, new ExtraFieldParsingBehavior() {
+ @Override
+ public ZipExtraField createExtraField(final ZipShort headerId) throws ZipException, InstantiationException, IllegalAccessException {
+ final ZipExtraField field = extraFieldFactory.apply(headerId);
+ return field == null ? parsingBehavior.createExtraField(headerId) : field;
+ }
+
+ @Override
+ public ZipExtraField fill(final ZipExtraField field,
+ final byte[] data, final int off, final int len,
+ final boolean local) throws ZipException {
+ return parsingBehavior.fill(field, data, off, len, local);
+ }
+
+ @Override
+ public ZipExtraField onUnparseableExtraField(
+ final byte[] data, final int off, final int len,
+ final boolean local, final int claimedLength) throws ZipException {
+ return parsingBehavior.onUnparseableExtraField(data, off, len, local, claimedLength);
+ }
+ });
+ }
+ return ExtraFieldUtils.parse(data, local, parsingBehavior);
+ }
+
/**
* Remove an extra field.
*
@@ -1334,7 +1380,7 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
* @param name the name to use
*/
protected void setName(String name) {
- if (name != null && getPlatform() == PLATFORM_FAT && !name.contains("/")) {
+ if (name != null && getPlatform() == PLATFORM_FAT && !name.contains(ZIP_DIR_SEP)) {
name = name.replace('\\', '/');
}
this.name = name;
@@ -1527,32 +1573,4 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry implements ArchiveEn
}
}
}
-
- private ZipExtraField[] parseExtraFields(final byte[] data, final boolean local,
- final ExtraFieldParsingBehavior parsingBehavior) throws ZipException {
- if (extraFieldFactory != null) {
- return ExtraFieldUtils.parse(data, local, new ExtraFieldParsingBehavior() {
- @Override
- public ZipExtraField createExtraField(final ZipShort headerId) throws ZipException, InstantiationException, IllegalAccessException {
- final ZipExtraField field = extraFieldFactory.apply(headerId);
- return field == null ? parsingBehavior.createExtraField(headerId) : field;
- }
-
- @Override
- public ZipExtraField fill(final ZipExtraField field,
- final byte[] data, final int off, final int len,
- final boolean local) throws ZipException {
- return parsingBehavior.fill(field, data, off, len, local);
- }
-
- @Override
- public ZipExtraField onUnparseableExtraField(
- final byte[] data, final int off, final int len,
- final boolean local, final int claimedLength) throws ZipException {
- return parsingBehavior.onUnparseableExtraField(data, off, len, local, claimedLength);
- }
- });
- }
- return ExtraFieldUtils.parse(data, local, parsingBehavior);
- }
}