Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/org/apache/commons/io/file/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/apache/commons/io/file/PathUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading