|
|
|
@ -4,85 +4,106 @@ import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
|
|
public class ZipUtils {
|
|
|
|
|
public static interface Filter {
|
|
|
|
|
boolean match(String file);
|
|
|
|
|
}
|
|
|
|
|
public static void ZipFolder(File srcFile, File zipFile, Filter filter) {
|
|
|
|
|
try {
|
|
|
|
|
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
|
|
ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter);
|
|
|
|
|
outZip.finish();
|
|
|
|
|
outZip.close();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ZipFolders(List<File> srcFiles, File zipFile, Filter filter) {
|
|
|
|
|
public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) {
|
|
|
|
|
ZipOutputStream outZip = null;
|
|
|
|
|
FileInputStream inputStream = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
|
|
for (File srcFile : srcFiles) {
|
|
|
|
|
ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter);
|
|
|
|
|
outZip = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
|
|
int len;
|
|
|
|
|
byte[] buffer = new byte[1024 * 256];
|
|
|
|
|
ZipEntry zipEntry = null;
|
|
|
|
|
|
|
|
|
|
File[] subFiles = srcDirectory.listFiles(filter);
|
|
|
|
|
if (subFiles != null && subFiles.length > 0) {
|
|
|
|
|
for (File subFile : subFiles) {
|
|
|
|
|
zipEntry = new ZipEntry(subFile.getName());
|
|
|
|
|
outZip.putNextEntry(zipEntry);
|
|
|
|
|
|
|
|
|
|
inputStream = new FileInputStream(subFile);
|
|
|
|
|
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
outZip.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
|
|
|
|
|
outZip.closeEntry();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
outZip.finish();
|
|
|
|
|
outZip.close();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
try {
|
|
|
|
|
outZip.finish();
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(outZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam, Filter filter) throws Exception {
|
|
|
|
|
if (zipOutputSteam == null)
|
|
|
|
|
return;
|
|
|
|
|
File file = new File(srcFileParentName + srcFileName);
|
|
|
|
|
public static void ZipFolders(Map<String, File> srcDirectories, File zipFile, FilenameFilter filter) {
|
|
|
|
|
ZipOutputStream outZip = null;
|
|
|
|
|
FileInputStream inputStream = null;
|
|
|
|
|
|
|
|
|
|
if (file.isFile()) {
|
|
|
|
|
ZipEntry zipEntry = new ZipEntry(srcFileName);
|
|
|
|
|
FileInputStream inputStream = new FileInputStream(file);
|
|
|
|
|
zipOutputSteam.putNextEntry(zipEntry);
|
|
|
|
|
try {
|
|
|
|
|
outZip = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
|
|
int len;
|
|
|
|
|
byte[] buffer = new byte[1024 * 256];
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
zipOutputSteam.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
zipOutputSteam.closeEntry();
|
|
|
|
|
} else {
|
|
|
|
|
// folder
|
|
|
|
|
String fileList[] = file.list();
|
|
|
|
|
// NO sub file
|
|
|
|
|
if (fileList.length <= 0) {
|
|
|
|
|
ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator);
|
|
|
|
|
zipOutputSteam.putNextEntry(zipEntry);
|
|
|
|
|
zipOutputSteam.closeEntry();
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
for (int i = 0; i < fileList.length; i++) {
|
|
|
|
|
if (filter != null && !filter.match(fileList[i])) {
|
|
|
|
|
ZipEntry zipEntry = null;
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, File> srcDirectory : srcDirectories.entrySet()) {
|
|
|
|
|
File[] subFiles = srcDirectory.getValue().listFiles(filter);
|
|
|
|
|
if (subFiles == null || subFiles.length == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam, filter);
|
|
|
|
|
for (File subFile : subFiles) {
|
|
|
|
|
zipEntry = new ZipEntry(srcDirectory.getKey() + File.separator + subFile.getName());
|
|
|
|
|
outZip.putNextEntry(zipEntry);
|
|
|
|
|
|
|
|
|
|
inputStream = new FileInputStream(subFile);
|
|
|
|
|
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
outZip.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
|
|
|
|
|
outZip.closeEntry();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
try {
|
|
|
|
|
outZip.finish();
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(outZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ZipFiles(List<String> srcFiles, File zipFile) {
|
|
|
|
|
|
|
|
|
|
ZipOutputStream outZip = null;
|
|
|
|
|
FileInputStream inputStream = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
|
|
outZip = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
|
|
// ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip);
|
|
|
|
|
int len = 0;
|
|
|
|
|
byte[] buffer = new byte[1024 * 256];
|
|
|
|
|
|
|
|
|
|
for (String path : srcFiles) {
|
|
|
|
@ -91,24 +112,29 @@ public class ZipUtils {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ZipEntry zipEntry = new ZipEntry(path);
|
|
|
|
|
FileInputStream inputStream = new FileInputStream(file);
|
|
|
|
|
inputStream = new FileInputStream(file);
|
|
|
|
|
outZip.putNextEntry(zipEntry);
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
outZip.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
|
|
|
|
|
outZip.closeEntry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outZip.finish();
|
|
|
|
|
outZip.close();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
if (outZip != null) {
|
|
|
|
|
try {
|
|
|
|
|
outZip.finish();
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(outZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|