欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java zip 压缩文件

程序员文章站 2022-05-15 12:41:38
...
1.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
public static boolean zip(String outputFileName, String inputFileName) {
ZipOutputStream out;
try {
out = new ZipOutputStream(new FileOutputStream(outputFileName));
zip(out, new File(inputFileName), "");
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void zip(String inputFileName) {
String fileName = inputFileName.substring(0, inputFileName.lastIndexOf("/"));
zip(fileName + ".zip", inputFileName);
}
private static void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
boolean flag = true;
if(dir!=null&&dir.getPath().split("/")[dir.getPath().split("/").length-1].equals(".svn")){
flag= false;
}
if(dir!=null&&dir.getPath().split("/")[dir.getPath().split("/").length-1].equals("logs")){
flag= false;
}
if(dir!=null&&dir.getPath().split("/")[dir.getPath().split("/").length-1].equals("lib")){
flag= false;
}
if(flag){
System.out.println(dir+"-----"+name);
}
return flag;
}

});
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
// System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
public static void unzip(String outputFileName) {
int index = outputFileName.lastIndexOf(".");
String inputFileName = outputFileName.substring(0, index);
unzip(outputFileName, inputFileName);
}
public static void unzip(String zipFilename, String outputDirectory) {
try {
File outFile = new File(outputDirectory);
if (!outFile.exists()) {
outFile.mkdirs();
}
ZipFile zipFile = new ZipFile(zipFilename);
Enumeration en = zipFile.entries();
ZipEntry zipEntry = null;
while (en.hasMoreElements()) {
zipEntry = (ZipEntry) en.nextElement();
if (zipEntry.isDirectory()) {
// mkdir directory
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);
File f = new File(outFile.getPath() + "/" + dirName);
f.mkdirs();
} else {
// unzip file
File f = new File(outFile.getPath() + "/" + zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.close();
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args){
System.currentTimeMillis();
zip("/home/liguojun/wowowo.zip","/home/liguojun/workspace/JShop/WebRoot/js");
//zip("/home/liguojun/workspace/JShop/WebRoot/js");
}
}