Skip to content

Instantly share code, notes, and snippets.

@shautzin
Created April 15, 2014 03:00
Show Gist options
  • Save shautzin/10698475 to your computer and use it in GitHub Desktop.
Save shautzin/10698475 to your computer and use it in GitHub Desktop.
Java ZipUtil
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by ShaoJin on 2014/4/15 0015.
*/
public class ZipUtil {
/**
* 压缩单个文件
*
* @param srcFile
* @param desFile
* @return
* @throws IOException
*/
static void zipFile(String srcFile, String desFile) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(desFile)));) {
zipFile(new File(srcFile), zos, "");
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
}
/**
* 压缩单个文件
*
* @param file
* @param zos
* @param path 路径
* @throws IOException
*/
static void zipFile(File file, ZipOutputStream zos, String path) throws IOException {
try (FileInputStream fis = new FileInputStream(file);) {
zos.putNextEntry(new ZipEntry(path + file.getName()));
int len;
byte[] buff = new byte[8096];
while ((len = fis.read(buff)) != -1) {
zos.write(buff, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
}
/**
* 压缩文件夹
*
* @param srcFolder
* @param desFile
* @return
*/
static void zipFolder(String srcFolder, String desFile) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(desFile)));) {
File folder = new File(srcFolder);
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
try (FileInputStream fis = new FileInputStream(files[i]);) {
zos.putNextEntry(new ZipEntry(folder.getName() + File.separator + files[i].getName()));
int len;
byte[] buff = new byte[8096];
while ((len = fis.read(buff)) != -1) {
zos.write(buff, 0, len);
}
} catch (Exception e) {
throw e;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
}
/**
* 循环压缩文件夹
*
* @param folder
* @param zos
* @param path 路径深度
*/
static void zipLoop(File folder, ZipOutputStream zos, String path) throws IOException {
File[] files = folder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
zipLoop(file, zos, path + file.getName() + File.separator);
} else {
zipFile(file, zos, path);
}
}
}
/**
* Public Method 压缩接口,将提供的文件全部压缩进目标zip包中
*
* @param srcFiles 文件数组,可以是文件夹,可以是文件
* @param descFile 压缩包的位置
* @return
*/
public static void zip(String[] srcFiles, String descFile) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(descFile)));) {
for (String srcFile : srcFiles) {
File file = new File(srcFile);
if (file.isDirectory()) {
zipLoop(file, zos, file.getName() + File.separator);
} else {
zipFile(file, zos, "");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
}
public static void main(String[] args) throws IOException {
// let's test it.
new File("D:\\3.zip").delete();
zip(new String[]{"D:\\application", "D:\\1.txt"}, "D:\\3.zip");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment