|
|
|
@ -6,6 +6,9 @@ import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.channels.Channels;
|
|
|
|
|
import java.nio.channels.FileChannel;
|
|
|
|
|
import java.nio.channels.WritableByteChannel;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
@ -15,6 +18,7 @@ public class ZipUtils {
|
|
|
|
|
|
|
|
|
|
public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) {
|
|
|
|
|
ZipOutputStream outZip = null;
|
|
|
|
|
WritableByteChannel writableByteChannel = null;
|
|
|
|
|
FileInputStream inputStream = null;
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
|
|
|
|
|
@ -22,8 +26,8 @@ public class ZipUtils {
|
|
|
|
|
fileOutputStream = new FileOutputStream(zipFile);
|
|
|
|
|
outZip = new ZipOutputStream(fileOutputStream);
|
|
|
|
|
|
|
|
|
|
int len;
|
|
|
|
|
byte[] buffer = new byte[1024 * 256];
|
|
|
|
|
writableByteChannel = Channels.newChannel(outZip);
|
|
|
|
|
|
|
|
|
|
ZipEntry zipEntry = null;
|
|
|
|
|
|
|
|
|
|
File[] subFiles = srcDirectory.listFiles(filter);
|
|
|
|
@ -34,8 +38,11 @@ public class ZipUtils {
|
|
|
|
|
|
|
|
|
|
inputStream = new FileInputStream(subFile);
|
|
|
|
|
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
outZip.write(buffer, 0, len);
|
|
|
|
|
FileChannel fileChannel = inputStream.getChannel();
|
|
|
|
|
try {
|
|
|
|
|
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
|
|
|
|
|
} finally {
|
|
|
|
|
FilesUtils.closeFriendly(fileChannel);
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
|
|
|
|
@ -52,20 +59,22 @@ public class ZipUtils {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(fileOutputStream);
|
|
|
|
|
FilesUtils.closeFriendly(writableByteChannel);
|
|
|
|
|
FilesUtils.closeFriendly(outZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ZipFolders(Map<String, File> srcDirectories, File zipFile, FilenameFilter filter) {
|
|
|
|
|
ZipOutputStream outZip = null;
|
|
|
|
|
WritableByteChannel writableByteChannel = null;
|
|
|
|
|
FileInputStream inputStream = null;
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fileOutputStream = new FileOutputStream(zipFile);
|
|
|
|
|
outZip = new ZipOutputStream(fileOutputStream);
|
|
|
|
|
int len;
|
|
|
|
|
byte[] buffer = new byte[1024 * 256];
|
|
|
|
|
writableByteChannel = Channels.newChannel(outZip);
|
|
|
|
|
|
|
|
|
|
ZipEntry zipEntry = null;
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, File> srcDirectory : srcDirectories.entrySet()) {
|
|
|
|
@ -79,8 +88,11 @@ public class ZipUtils {
|
|
|
|
|
|
|
|
|
|
inputStream = new FileInputStream(subFile);
|
|
|
|
|
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
outZip.write(buffer, 0, len);
|
|
|
|
|
FileChannel fileChannel = inputStream.getChannel();
|
|
|
|
|
try {
|
|
|
|
|
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
|
|
|
|
|
} finally {
|
|
|
|
|
FilesUtils.closeFriendly(fileChannel);
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
|
|
|
|
@ -97,7 +109,9 @@ public class ZipUtils {
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilesUtils.closeFriendly(fileOutputStream);
|
|
|
|
|
FilesUtils.closeFriendly(writableByteChannel);
|
|
|
|
|
FilesUtils.closeFriendly(outZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -105,13 +119,13 @@ public class ZipUtils {
|
|
|
|
|
public static void ZipFiles(List<String> srcFiles, File zipFile) {
|
|
|
|
|
|
|
|
|
|
ZipOutputStream outZip = null;
|
|
|
|
|
WritableByteChannel writableByteChannel = null;
|
|
|
|
|
FileInputStream inputStream = null;
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
|
try {
|
|
|
|
|
fileOutputStream = new FileOutputStream(zipFile);
|
|
|
|
|
outZip = new ZipOutputStream(fileOutputStream);
|
|
|
|
|
int len = 0;
|
|
|
|
|
byte[] buffer = new byte[1024 * 256];
|
|
|
|
|
writableByteChannel = Channels.newChannel(outZip);
|
|
|
|
|
|
|
|
|
|
for (String path : srcFiles) {
|
|
|
|
|
File file = new File(path);
|
|
|
|
@ -119,12 +133,16 @@ public class ZipUtils {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ZipEntry zipEntry = new ZipEntry(srcFiles.size() > 1 ? path.substring(1) : file.getName());
|
|
|
|
|
inputStream = new FileInputStream(file);
|
|
|
|
|
|
|
|
|
|
outZip.putNextEntry(zipEntry);
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
outZip.write(buffer, 0, len);
|
|
|
|
|
|
|
|
|
|
inputStream = new FileInputStream(file);
|
|
|
|
|
FileChannel fileChannel = inputStream.getChannel();
|
|
|
|
|
try {
|
|
|
|
|
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
|
|
|
|
|
} finally {
|
|
|
|
|
FilesUtils.closeFriendly(fileChannel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outZip.closeEntry();
|
|
|
|
|
FilesUtils.closeFriendly(inputStream);
|
|
|
|
|
}
|
|
|
|
@ -141,6 +159,7 @@ public class ZipUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
FilesUtils.closeFriendly(fileOutputStream);
|
|
|
|
|
FilesUtils.closeFriendly(writableByteChannel);
|
|
|
|
|
FilesUtils.closeFriendly(outZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|