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 2025/04/14 12:36:57 UTC
(commons-compress) 02/04: Reuse PathUtils#getBaseName(Path)
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
commit d4615b412b256317bcdff3bf018ff60d5355409e
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Mon Apr 14 08:29:21 2025 -0400
Reuse PathUtils#getBaseName(Path)
Deprecate FileNameUtils#getBaseName(Path)
---
src/changes/changes.xml | 1 +
.../commons/compress/archivers/zip/ZipSplitOutputStream.java | 8 +++++---
.../archivers/zip/ZipSplitReadOnlySeekableByteChannel.java | 3 ++-
.../java/org/apache/commons/compress/utils/FileNameUtils.java | 10 ++++------
4 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6a7573e62..2ffea2eac 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -84,6 +84,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.unpack200.bytecode.forms.NewClassRefForm.setByteCodeOperands(ByteCode, OperandManager, int) now throws Pack200Exception instead of Error on some bad inputs and states.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.unpack200.bytecode.forms.ReferenceForm.setByteCodeOperands(ByteCode, OperandManager, int) now throws Pack200Exception instead of Error on some bad inputs and states.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate org.apache.commons.compress.harmony.pack200.CanonicalCodecFamilies.CanonicalCodecFamilies().</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate FileNameUtils#getBaseName(Path).</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.getModificationInstant().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.setModificationInstant(Instant).</action>
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java
index 26191bbca..8cb8fa04e 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java
@@ -32,7 +32,7 @@
import java.util.Objects;
import java.util.TreeMap;
-import org.apache.commons.compress.utils.FileNameUtils;
+import org.apache.commons.io.file.PathUtils;
/**
* Used internally by {@link ZipArchiveOutputStream} when creating a split archive.
@@ -152,8 +152,9 @@ private void finish() throws IOException {
if (finished) {
throw new IOException("This archive has already been finished");
}
+ final Path path = zipFile;
- final String zipFileBaseName = FileNameUtils.getBaseName(zipFile);
+ final String zipFileBaseName = PathUtils.getBaseName(path);
outputStream.close();
Files.move(zipFile, zipFile.resolveSibling(zipFileBaseName + ".zip"), StandardCopyOption.ATOMIC_MOVE);
finished = true;
@@ -169,7 +170,8 @@ public int getCurrentSplitSegmentIndex() {
private Path getSplitSegmentFileName(final Integer zipSplitSegmentSuffixIndex) {
final int newZipSplitSegmentSuffixIndex = zipSplitSegmentSuffixIndex == null ? currentSplitSegmentIndex + 2 : zipSplitSegmentSuffixIndex;
- final String baseName = FileNameUtils.getBaseName(zipFile);
+ final Path path = zipFile;
+ final String baseName = PathUtils.getBaseName(path);
final StringBuilder extension = new StringBuilder(".z");
if (newZipSplitSegmentSuffixIndex <= 9) {
extension.append("0").append(newZipSplitSegmentSuffixIndex);
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java
index 9deff11f5..bf811c3d5 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java
@@ -39,6 +39,7 @@
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.utils.FileNameUtils;
import org.apache.commons.compress.utils.MultiReadOnlySeekableByteChannel;
+import org.apache.commons.io.file.PathUtils;
/**
* {@link MultiReadOnlySeekableByteChannel} that knows what a split ZIP archive should look like.
@@ -98,7 +99,7 @@ public static SeekableByteChannel buildFromLastSplitSegment(final Path lastSegme
throw new IllegalArgumentException("The extension of last ZIP split segment should be .zip");
}
final Path parent = Objects.nonNull(lastSegmentPath.getParent()) ? lastSegmentPath.getParent() : lastSegmentPath.getFileSystem().getPath(".");
- final String fileBaseName = FileNameUtils.getBaseName(lastSegmentPath);
+ final String fileBaseName = PathUtils.getBaseName(lastSegmentPath);
final ArrayList<Path> splitZipSegments;
// ZIP split segments should be like z01,z02....z(n-1) based on the ZIP specification
final Pattern pattern = Pattern.compile(Pattern.quote(fileBaseName) + ".[zZ][0-9]+");
diff --git a/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java b/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java
index 1df0d1965..b7240eaa4 100644
--- a/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java
+++ b/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java
@@ -23,6 +23,7 @@
import java.nio.file.Path;
import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.file.PathUtils;
/**
* Generic file name utilities.
@@ -40,14 +41,11 @@ public class FileNameUtils {
* @return the base name of file name
* @param path the path of the file to obtain the base name of.
* @since 1.22
+ * @deprecated Use {@link PathUtils#getBaseName(Path)}.
*/
+ @Deprecated
public static String getBaseName(final Path path) {
- // TODO Use Commons IO 2.16.0
- if (path == null) {
- return null;
- }
- final Path fileName = path.getFileName();
- return fileName != null ? FilenameUtils.removeExtension(fileName.toString()) : null;
+ return PathUtils.getBaseName(path);
}
/**