写好的代码,可以压缩成.zip格式了,可是压缩成的.zip文件老带着原文件的路经,不要这个路经变成包,要的是直接的内容,请高手指点: import org.apache.tools.zip.*;
import java.io.*;
import java.util.Enumeration; public class AntZip { private ZipFile zipFile;
private ZipOutputStream zipOut; //压缩Zip
private ZipEntry zipEntry;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes; public AntZip(){
this(512);
} public AntZip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
} //压缩文件夹内的文件
public void doZip(String zipDirectory){//zipDirectoryPath:需要压缩的文件夹名
File file;
File zipDir;
zipDir = new File("E:\\ebook_zip\\"+zipDirectory);
//System.out.println(zipDir);
String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("E:\\ebook_zip\\"+zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
} //由doZip调用,递归完成目录文件读取
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){//如果目录为空,则单独创建之.
//ZipEntry的isDirectory()方法中,目录以"/"结尾.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{//如果目录不为空,则分别处理目录和文件.
for(File fileName : files){
System.out.println(fileName);
if(fileName.isDirectory()){
handleDir(fileName , this.zipOut);
}
else{
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
while((this.readedBytes = fileIn.read(this.buf))>0){
this.zipOut.write(this.buf , 0 , this.readedBytes);
}
this.zipOut.closeEntry();
}
}
}
}
import java.io.*;
import java.util.Enumeration; public class AntZip { private ZipFile zipFile;
private ZipOutputStream zipOut; //压缩Zip
private ZipEntry zipEntry;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes; public AntZip(){
this(512);
} public AntZip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
} //压缩文件夹内的文件
public void doZip(String zipDirectory){//zipDirectoryPath:需要压缩的文件夹名
File file;
File zipDir;
zipDir = new File("E:\\ebook_zip\\"+zipDirectory);
//System.out.println(zipDir);
String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("E:\\ebook_zip\\"+zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
} //由doZip调用,递归完成目录文件读取
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){//如果目录为空,则单独创建之.
//ZipEntry的isDirectory()方法中,目录以"/"结尾.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{//如果目录不为空,则分别处理目录和文件.
for(File fileName : files){
System.out.println(fileName);
if(fileName.isDirectory()){
handleDir(fileName , this.zipOut);
}
else{
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
while((this.readedBytes = fileIn.read(this.buf))>0){
this.zipOut.write(this.buf , 0 , this.readedBytes);
}
this.zipOut.closeEntry();
}
}
}
}
http://download.csdn.net/source/1725358
http://download.csdn.net/source/1725358