mirror of https://github.com/halo-dev/halo
Accomplish zip utility
parent
95b3e944f0
commit
a041409ff7
|
@ -1,12 +1,15 @@
|
||||||
package run.halo.app.utils;
|
package run.halo.app.utils;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import run.halo.app.exception.ForbiddenException;
|
import run.halo.app.exception.ForbiddenException;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
|
@ -16,6 +19,7 @@ import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* File utilities.
|
* File utilities.
|
||||||
|
@ -117,6 +121,54 @@ public class FileUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zip folder or file.
|
||||||
|
*
|
||||||
|
* @param fileToZip file path to zip must not be null
|
||||||
|
* @param zipOut zip output stream must not be null
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static void zip(@NonNull Path fileToZip, @NonNull ZipOutputStream zipOut) throws IOException {
|
||||||
|
// Zip file
|
||||||
|
zip(fileToZip, fileToZip.getFileName().toString(), zipOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zip folder or file.
|
||||||
|
*
|
||||||
|
* @param fileToZip file path to zip must not be null
|
||||||
|
* @param fileName file name must not be blank
|
||||||
|
* @param zipOut zip output stream must not be null
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static void zip(@NonNull Path fileToZip, @NonNull String fileName, @NonNull ZipOutputStream zipOut) throws IOException {
|
||||||
|
if (Files.isDirectory(fileToZip)) {
|
||||||
|
log.debug("Try to zip folder: [{}]", fileToZip);
|
||||||
|
// Append with '/' if missing
|
||||||
|
String folderName = StringUtils.appendIfMissing(fileName, File.separator, File.separator);
|
||||||
|
// Create zip entry and put into zip output stream
|
||||||
|
zipOut.putNextEntry(new ZipEntry(folderName));
|
||||||
|
// Close entry for writing the next entry
|
||||||
|
zipOut.closeEntry();
|
||||||
|
|
||||||
|
// Iterate the sub files recursively
|
||||||
|
List<Path> subFiles = Files.list(fileToZip).collect(Collectors.toList());
|
||||||
|
for (Path subFileToZip : subFiles) {
|
||||||
|
zip(subFileToZip, folderName + subFileToZip.getFileName(), zipOut);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Open file to be zipped
|
||||||
|
try (InputStream inputStream = Files.newInputStream(fileToZip)) {
|
||||||
|
// Create zip entry for target file
|
||||||
|
ZipEntry zipEntry = new ZipEntry(fileName);
|
||||||
|
// Put the entry into zip output stream
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
// Copy
|
||||||
|
IOUtils.copy(inputStream, zipOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unzips content to the target path.
|
* Unzips content to the target path.
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
package run.halo.app.utils;
|
package run.halo.app.utils;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
@ -16,6 +20,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 19-4-19
|
* @date 19-4-19
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class FileUtilsTest {
|
public class FileUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -57,4 +62,30 @@ public class FileUtilsTest {
|
||||||
|
|
||||||
Assert.assertTrue(Files.notExists(tempDirectory));
|
Assert.assertTrue(Files.notExists(tempDirectory));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void zipFolderTest() throws IOException {
|
||||||
|
// Create some temporary files
|
||||||
|
Path rootFolder = Files.createTempDirectory("zip-root-");
|
||||||
|
log.debug("Folder name: [{}]", rootFolder.getFileName());
|
||||||
|
Files.createTempFile(rootFolder, "zip-file1-", ".txt");
|
||||||
|
Files.createTempFile(rootFolder, "zip-file2-", ".txt");
|
||||||
|
Path subRootFolder = Files.createTempDirectory(rootFolder, "zip-subroot-");
|
||||||
|
Files.createTempFile(subRootFolder, "zip-subfile1-", ".txt");
|
||||||
|
Files.createTempFile(subRootFolder, "zip-subfile2-", ".txt");
|
||||||
|
|
||||||
|
// Create target file
|
||||||
|
Path zipToStore = Files.createTempFile("zipped-", ".zip");
|
||||||
|
// Create zip output stream
|
||||||
|
try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipToStore))) {
|
||||||
|
// Zip file
|
||||||
|
FileUtils.zip(rootFolder, zipOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Clear the test folder created before
|
||||||
|
FileUtils.deleteFolder(rootFolder);
|
||||||
|
Files.delete(zipToStore);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -17,7 +17,6 @@ import static org.junit.Assert.assertTrue;
|
||||||
*/
|
*/
|
||||||
public class PathsTest {
|
public class PathsTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTest() {
|
public void getTest() {
|
||||||
Path path = Paths.get("/home/test/", "/upload/test.txt");
|
Path path = Paths.get("/home/test/", "/upload/test.txt");
|
||||||
|
|
Loading…
Reference in New Issue