Fix PathUtils.copyFileToDirectory(URL,Path,CopyOption[]).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -192,6 +192,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-776" dev="kinow" type="fix" due-to="Chris Povirk">
Fix parameters to requireNonNull call in DeferredOutputSteam #368.
</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ Fix PathUtils.copyFileToDirectory(URL,Path,CopyOption[]).
+ </action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">
Add GitHub coverage.yml.

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.Uncheck;
import org.apache.commons.io.file.Counters.PathCounters;
@@ -310,8 +311,9 @@ public static Path copyFileToDirectory(final Path sourceFile, final Path targetD
*/
public static Path copyFileToDirectory(final URL sourceFile, final Path targetDirectory, final CopyOption... copyOptions) throws IOException {
try (InputStream inputStream = sourceFile.openStream()) {
- Files.copy(inputStream, targetDirectory.resolve(sourceFile.getFile()), copyOptions);
- return targetDirectory;
+ final Path resolve = targetDirectory.resolve(FilenameUtils.getName(sourceFile.getFile()));
+ Files.copy(inputStream, resolve, copyOptions);
+ return resolve;
}
}

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.util.Map;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.test.TestUtils;
import org.apache.commons.lang3.ArrayUtils;
@@ -154,8 +156,7 @@ public void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse() th
@Test
public void testCopyDirectoryForDifferentFilesystemsWithRelativePath() throws IOException {
final Path archivePath = Paths.get(TEST_JAR_PATH);
- try (FileSystem archive = openArchive(archivePath, false);
- final FileSystem targetArchive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
+ try (FileSystem archive = openArchive(archivePath, false); final FileSystem targetArchive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
final Path targetDir = targetArchive.getPath("targetDir");
Files.createDirectory(targetDir);
// relative jar -> relative dir
@@ -195,6 +196,15 @@ public void testCopyFile() throws IOException {
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");
+ final URL url = new URL("file:///" + FilenameUtils.getPath(sourceFile.toAbsolutePath().toString()) + sourceFile.getFileName());
+ final Path targetFile = PathUtils.copyFileToDirectory(url, tempDirPath);
+ assertTrue(Files.exists(targetFile));
+ assertEquals(Files.size(sourceFile), Files.size(targetFile));
+ }
+
@Test
public void testCreateDirectoriesAlreadyExists() throws IOException {
assertEquals(tempDirPath.getParent(), PathUtils.createParentDirectories(tempDirPath));

Fix PathUtils.copyFileToDirectory(URL,Path,CopyOption[]).