Initial commit
This commit is contained in:
49
src/com/flaremicro/util/ZipUtils.java
Normal file
49
src/com/flaremicro/util/ZipUtils.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.flaremicro.util;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZipUtils {
|
||||
|
||||
|
||||
public static void zipDirectory(File sourceDir, File zipFile) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(zipFile);
|
||||
ZipOutputStream zos = new ZipOutputStream(fos);
|
||||
try {
|
||||
zipFilesRecursively(sourceDir, sourceDir, zos);
|
||||
} finally {
|
||||
zos.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void zipFilesRecursively(File rootDir, File source, ZipOutputStream zos) throws IOException {
|
||||
if (source.isDirectory()) {
|
||||
File[] files = source.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
zipFilesRecursively(rootDir, file, zos);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
FileInputStream fis = new FileInputStream(source);
|
||||
try {
|
||||
// Create the relative path for the entry
|
||||
String zipEntryName = source.getAbsolutePath().substring(rootDir.getAbsolutePath().length() + 1).replace("\\", "/");
|
||||
ZipEntry zipEntry = new ZipEntry(zipEntryName);
|
||||
zos.putNextEntry(zipEntry);
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
zos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
zos.closeEntry();
|
||||
} finally {
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user