Skip to content

Commit

Permalink
Fix unzip subfolders
Browse files Browse the repository at this point in the history
  • Loading branch information
ebocher committed Oct 9, 2023
1 parent 4f05baf commit 226a9f4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ public static List<String> listFiles(File directory) throws IOException {
}
return result;
}

/**
* Unzip to a directory
*
* @param zipFile the zipped file
* @throws FileNotFoundException
* @throws IOException
*/
public static void unzip(File zipFile) throws IOException {
Path parentDir = zipFile.toPath().getParent();
String fileName = zipFile.getName();
String withoutExtension = fileName.substring(0, fileName.lastIndexOf(".")).replace(".", "_");
File targetDir = parentDir.resolve(withoutExtension).toFile();
if(!targetDir.exists()){
targetDir.mkdir();
}
unzip(zipFile, targetDir);
}

/**
* Unzip to a directory
Expand All @@ -149,7 +167,7 @@ public static List<String> listFiles(File directory) throws IOException {
* @throws FileNotFoundException
* @throws IOException
*/
public static void unzipFile(File zipFile, File directory) throws IOException {
public static void unzip(File zipFile, File directory) throws IOException {
if (directory == null) {
throw new IOException("The directory cannot be null");
}
Expand Down Expand Up @@ -177,15 +195,27 @@ else if (!zipFile.exists()){
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = newFile(directory, zipEntry);
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
// fix for Windows-created archives
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}

// write file content
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
fos.close();
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
} finally {
if (zis != null) {
zis.close();
Expand Down Expand Up @@ -266,6 +296,23 @@ else if (outFile.exists()){
}
}

/**
* Zips the specified file or folder
*
* @param toZip
* @throws IOException
*/
public static void zip(File toZip) throws IOException {
Path parentDir = toZip.toPath().getParent();
String fileName = toZip.getName();
int lastIndex = fileName.lastIndexOf(".");
if(lastIndex!=-1) {
fileName = fileName.substring(0, lastIndex).replace(".", "_");
}
Path targetDir = parentDir.resolve(fileName+".zip");
zip(toZip, targetDir.toFile());
}

/**
* Zips the specified file or folder
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public void zipFolder() throws Exception {
}
File tmpFile = File.createTempFile("test", ".txt", directory);
assertTrue(tmpFile.exists());
File tmpFile2 = File.createTempFile("test2", ".txt", directory);
assertTrue(tmpFile2.exists());
File outPutZip = new File("./target/output.zip");
outPutZip.delete();
FileUtilities.zip(directory, outPutZip);
Expand All @@ -171,13 +173,41 @@ public void zipUnZipFiles() throws Exception {
}
File tmpFile = File.createTempFile("test", ".txt", directory);
assertTrue(tmpFile.exists());
File subFolder = new File(directory.getAbsolutePath()+File.separator+"subFolder");
subFolder.mkdir();
File tmpFile2 = File.createTempFile("second_file", ".txt", subFolder);
assertTrue(tmpFile2.exists());
File outPutZip = new File("./target/output.zip");
outPutZip.delete();
FileUtilities.zip(new File[]{tmpFile}, outPutZip);
tmpFile.delete();
assertTrue(outPutZip.exists());
FileUtilities.unzipFile(outPutZip, directory);
assertTrue(tmpFile.exists());
}

assertTrue(outPutZip.exists());
FileUtilities.unzip(outPutZip, directory);
assertTrue(tmpFile.exists());
}

@Test
public void zipUnZip() throws Exception {
File directory = new File("/tmp/directory");
if (directory.exists()) {
FileUtilities.deleteFiles(directory);
} else {
directory.mkdir();
}
File tmpFile = File.createTempFile("test", ".txt", directory);
assertTrue(tmpFile.exists());
File subFolder = new File(directory.getAbsolutePath()+File.separator+"subFolder");
subFolder.mkdir();
File tmpFile2 = File.createTempFile("second_file", ".txt", subFolder);
assertTrue(tmpFile2.exists());
File outputZip = new File(directory.toPath().getParent().toFile().getAbsolutePath() + File.separator + "directory.zip");
outputZip.delete();
FileUtilities.zip(directory);
assertTrue(outputZip.exists());
FileUtilities.deleteFiles(directory, true);
assertFalse(tmpFile.exists());
assertFalse(subFolder.exists());
FileUtilities.unzip(outputZip);
}

}

0 comments on commit 226a9f4

Please sign in to comment.