import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zip{
private void toZip(String fileName,String FilePath){
try {
ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(fileName));
File file = new File(FilePath);
if(file.exists()&&file.isDirectory()){
File[] file1 = file.listFiles();
for(File file2: file1){
String file2Name = file2.getName();
byte[] byt = new byte[(int) file2.length()];
FileInputStream fi = new FileInputStream(file2);
fi.read(byt);
zo.putNextEntry(new ZipEntry(file2Name));
zo.write(byt);
}
zo.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Zip zip = new Zip();
zip.toZip("d://lick.zip","d://lick");
}
}
用ant的:
package zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipTool {
public void toZip(String fileName,String filePath){
File file = new File(filePath);
ZipOutputStream out = null;
System.out.println(System.currentTimeMillis());
try {
out = new ZipOutputStream(
new FileOutputStream (fileName)
);
toZip(out,file,filePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void toZip(ZipOutputStream out,File file,String filePath){
if(file.isDirectory()){
try {
String afterReplace = file.getPath().replace(filePath, "");
if(!afterReplace.equals("")){
afterReplace=afterReplace.substring(1);
// System.out.println("afterReplace111"+afterReplace);
// afterReplace=afterReplace.replace(file.getName(),"");
}
System.out.println("第一个"+afterReplace+"/");
out.putNextEntry(new ZipEntry(afterReplace+"/"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(File singleFile:file.listFiles()){
toZip(out,singleFile,filePath);
}
}else{
try {
// String[] name = file.getName().split("/");
//
// String splitName = name[name.length-1];
//
String splitName = file.getPath().replace(filePath,"");
if(!splitName.equals("")){
splitName=splitName.substring(1);
// splitName=splitName.replaceAll("////","/");
}
System.out.println("第二个"+splitName);
out.putNextEntry(new ZipEntry(splitName));
FileInputStream fi = new FileInputStream(file);
int len;
while((len = fi.read())!= -1){
out.write(len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// public void toZip(String fileName,String filePath) throws IOException{
// File file = new File(filePath);
// ZipOutputStream op = null;
// try {
// op = new ZipOutputStream(
// new FileOutputStream(fileName)
// );
// } catch (FileNotFoundException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// for(File singleFile:file.listFiles()){
// if(singleFile.isDirectory()){
// op.putNextEntry(new ZipEntry(singleFile.getName()+"/"));
// toZip();
// }else{
// try {
// int len;
// FileInputStream fi = new FileInputStream(singleFile.getPath());
// op.putNextEntry(new ZipEntry(singleFile.getName()));
// while((len = fi.read())!= -1){
// op.write(len);
// }
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
//
// op.close();
// }
public static void main(String[] args) throws IOException {
ZipTool zipTool = new ZipTool();
String fileName ="D://lick.zip";
String filePath ="D://lick";
zipTool.toZip(fileName, filePath);
}
}