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-io.git


The following commit(s) were added to refs/heads/master by this push:
     new a92414022 IO-870: PathUtils.copyFileToDirectory (#728)
a92414022 is described below

commit a92414022d6b0b3b70ad9ed13e8137ce8b38050a
Author: David Smiley <dsmi...@apache.org>
AuthorDate: Thu Mar 20 10:10:19 2025 -0400

    IO-870: PathUtils.copyFileToDirectory (#728)
    
    * IO-870: PathUtils.copyFileToDirectory
    span FileSystem.
    
    * Add test
    
    * null check
---
 src/main/java/org/apache/commons/io/file/PathUtils.java     | 13 ++++++++++++-
 src/test/java/org/apache/commons/io/file/PathUtilsTest.java | 10 ++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java 
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 2cda505aa..9abdb361e 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -321,7 +321,18 @@ public static Path copyFile(final URL sourceFile, final 
Path targetFile, final C
      * @see Files#copy(Path, Path, CopyOption...)
      */
     public static Path copyFileToDirectory(final Path sourceFile, final Path 
targetDirectory, final CopyOption... copyOptions) throws IOException {
-        return Files.copy(sourceFile, 
targetDirectory.resolve(sourceFile.getFileName()), copyOptions);
+        // Path.resolve() naturally won't work across FileSystem unless we 
convert to a String
+        final Path sourceFileName = sourceFile.getFileName();
+        if (sourceFileName == null) {
+            throw new IllegalArgumentException("must have a file name: " + 
sourceFile);
+        }
+        final Path targetFile;
+        if (sourceFileName.getFileSystem() == targetDirectory.getFileSystem()) 
{
+            targetFile = targetDirectory.resolve(sourceFileName);
+        } else {
+            targetFile = targetDirectory.resolve(sourceFileName.toString());
+        }
+        return Files.copy(sourceFile, targetFile, copyOptions);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java 
b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index 787a13dcd..f3e6954fb 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -178,6 +178,16 @@ public void testCopyFile() throws IOException {
         assertEquals(Files.size(sourceFile), Files.size(targetFile));
     }
 
+    @Test
+    public void testCopyFileTwoFileSystem() throws IOException {
+        try (FileSystem archive = openArchive(Paths.get(TEST_JAR_PATH), 
false)) {
+            final Path sourceFile = archive.getPath("next/dir/test.log");
+            final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, 
tempDirPath);
+            assertTrue(Files.exists(targetFile));
+            assertEquals(Files.size(sourceFile), Files.size(targetFile));
+        }
+    }
+
     @Test
     public void testCopyURL() throws IOException {
         final Path sourceFile = 
Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");

Reply via email to