From c32acb2ec72bfb57492518318431311cc300c702 Mon Sep 17 00:00:00 2001 From: Susan Eraly Date: Mon, 30 Dec 2019 16:48:57 -0800 Subject: [PATCH] fix if dir does not exist (#129) * fix if dir does not exist Signed-off-by: eraly * added simple test Signed-off-by: eraly --- .../main/java/org/nd4j/util/ArchiveUtils.java | 4 +- .../org/nd4j/resources/TestArchiveUtils.java | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 nd4j/nd4j-common/src/test/java/org/nd4j/resources/TestArchiveUtils.java diff --git a/nd4j/nd4j-common/src/main/java/org/nd4j/util/ArchiveUtils.java b/nd4j/nd4j-common/src/main/java/org/nd4j/util/ArchiveUtils.java index d9566aabe..f0c6ef318 100644 --- a/nd4j/nd4j-common/src/main/java/org/nd4j/util/ArchiveUtils.java +++ b/nd4j/nd4j-common/src/main/java/org/nd4j/util/ArchiveUtils.java @@ -24,8 +24,6 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.nd4j.base.Preconditions; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.*; import java.util.ArrayList; @@ -56,6 +54,8 @@ public class ArchiveUtils { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); + if (!new File(dest).exists()) + new File(dest).mkdirs(); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; diff --git a/nd4j/nd4j-common/src/test/java/org/nd4j/resources/TestArchiveUtils.java b/nd4j/nd4j-common/src/test/java/org/nd4j/resources/TestArchiveUtils.java new file mode 100644 index 000000000..9a36d7fe3 --- /dev/null +++ b/nd4j/nd4j-common/src/test/java/org/nd4j/resources/TestArchiveUtils.java @@ -0,0 +1,51 @@ +package org.nd4j.resources; + +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.nd4j.util.ArchiveUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class TestArchiveUtils { + @Rule + public TemporaryFolder testDir = new TemporaryFolder(); + + @Test + public void testUnzipFileTo() throws IOException { + //random txt file + File dir = testDir.newFolder(); + String content = "test file content"; + String path = "myDir/myTestFile.txt"; + File testFile = new File(dir, path); + testFile.getParentFile().mkdir(); + FileUtils.writeStringToFile(testFile, content, StandardCharsets.UTF_8); + + //zip it as test.zip + File zipFile = new File(testFile.getParentFile(),"test.zip"); + FileOutputStream fos = new FileOutputStream(zipFile); + ZipOutputStream zipOut = new ZipOutputStream(fos); + FileInputStream fis = new FileInputStream(testFile); + ZipEntry zipEntry = new ZipEntry(testFile.getName()); + zipOut.putNextEntry(zipEntry); + byte[] bytes = new byte[1024]; + int length; + while((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + zipOut.close(); + fis.close(); + fos.close(); + + //now unzip to a directory that doesn't previously exist + File unzipDir = new File(testFile.getParentFile(),"unzipTo"); + ArchiveUtils.unzipFileTo(zipFile.getAbsolutePath(),unzipDir.getAbsolutePath()); + } +}