org.springframework.util.ZipUtils Maven / Gradle / Ivy
package org.springframework.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Deque;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
public final class ZipUtils {
/**
* ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^\w\d\s])(?=\S+$).{8,24}$
*
* ^ : start-of-string
* (?=.*[0-9]) : a digit must occur at least once
* (?=.*[a-z]) : a lower case letter must occur at least once
* (?=.*[A-Z]) : a upper case letter must occur at least once
* (?=.*[^\w\d\s]) : a special character must occur at least once
* (?=\S+$) : no whitespace allowed in the entire string
* .{8,24} : anything, at least 8~24 length
* $ : end-of-string
*/
public static void zip(File directory, File file) {
zip(directory, file, ".*", true);
}
/**
* @see ^\\S+.(?i)(java)$
*/
public static void zip(File directory, File file, String pattern, boolean include) {
Deque deque = new LinkedList();
deque.push(directory);
URI uri = directory.toURI();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(file))) {
while (deque.size() > 0) {
File[] listFiles = (directory = deque.pop()) == null ? null : directory.listFiles();
if (listFiles == null) {
continue;
}
for (File listFile : listFiles) {
String name = uri.relativize(listFile.toURI()).getPath();
if (listFile.isDirectory()) {
deque.push(listFile);
zipOutputStream.putNextEntry(new ZipEntry(name));
}
else {
boolean matches = listFile.getName().matches(pattern);
if (matches && !include || (!matches && include)) {
continue;
}
zipOutputStream.putNextEntry(new ZipEntry(name));
try (InputStream inputStream = new FileInputStream(listFile)) {
StreamUtils.copy(inputStream, zipOutputStream);
}
zipOutputStream.closeEntry();
}
}
}
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
public static void unzip(File zip, File directory) {
try (ZipFile zipFile = new ZipFile(zip)) {
for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
ZipEntry nextElement = entries.nextElement();
File file = Paths.get(directory.getPath(), nextElement.getName()).toFile();
if (nextElement.getName().endsWith("/")) {
Assert.isTrue(file != null && (file.mkdirs() || file.isDirectory()), "Create Directory fail..." + file);
}
else {
Assert.isTrue(file != null && file.getParentFile() != null && (file.getParentFile().mkdirs() || file.getParentFile().isDirectory()), "Create Directory fail..." + file);
FileCopyUtils.copy(zipFile.getInputStream(nextElement), new FileOutputStream(file));
try (InputStream inputStream = zipFile.getInputStream(nextElement); OutputStream outputStream = new FileOutputStream(file)) {
StreamUtils.copy(inputStream, outputStream);
}
}
}
}
catch (IOException | IllegalArgumentException e) {
throw new IllegalStateException(e);
}
}
}