/*
* 压缩文件夹
*/
private void compressionFolder(File file, ZipOutputStream zipOutputStream) throws FileNotFoundException {
FileInputStream fileInputStream = null;
File[] files = null;
try {
files = file.listFiles(); //得到该文件夹下的所有文件
for (File filesFile : files) {
if(filesFile.isDirectory()) {
zipOutputStream.putNextEntry(new ZipEntry(filesFile.getName()));
compressionFolder(file, zipOutputStream);
} else {
compressionFile(file, zipOutputStream);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
压缩文件夹不成功。在压缩文件夹中这句zipOutputStream.putNextEntry(new ZipEntry(filesFile.getName()));
有问题
if (filesFile.isDirectory()) {
zipOutputStream.putNextEntry(new ZipEntry(filesFile.getName() + "/"));
File[] files = filesFile.listFiles();
for (File file : files) {
compressionFolder(file, zipOutputStream);
}
}
######我试了一下,不行。我想在这个函数中做递归。######public class Compression {
// 要压缩文件的路径
private String srcPath;
// 压缩后存放的路径
private String dirPath;
public Compression(String srcPath, String dirPath) {
this.srcPath = srcPath;
this.dirPath = dirPath;
}
/**
* 压缩程序
/
public void compressionProgram() {
ZipOutputStream zipOutputStream = null;
File file = null;
try {
file = new File(srcPath);
zipOutputStream = new ZipOutputStream(new FileOutputStream(dirPath));
if(file.isDirectory()) {//文件夹
compressionFolder(file, zipOutputStream, "");
} else {//文件
zipOutputStream.putNextEntry(new ZipEntry(""));
compressionFile(file, zipOutputStream, "");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(zipOutputStream != null) {
zipOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} // try
}
/**
压缩文件
* 参数: 当前访问的文件file, 压缩流zipCompression
/
private void compressionFile(File file, ZipOutputStream zipOutputStream, String dir) {
FileInputStream fileInputStream = null;
try {
zipOutputStream.putNextEntry(new ZipEntry(dir + file.getName()));
fileInputStream = new FileInputStream(file);
byte[] byteStr = new byte[1024 * 1024]; // 每次读取1M
int strLength = -1;
while((strLength = fileInputStream.read(byteStr) ) > 0) {
zipOutputStream.write(byteStr, 0, strLength);
}
} catch (FileNotFoundException e) {
new Throwable("文件不存在!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} // try
}
/**
压缩文件夹
*/
private void compressionFolder(File file, ZipOutputStream zipOutputStream, String dir) throws FileNotFoundException {
String path = dir + file.getName() + "/";
try {
zipOutputStream.putNextEntry(new ZipEntry(path));
File[] files = file.listFiles(); //得到该文件夹下的所有文件
for (File filesFile : files) {
if(filesFile.isDirectory()) {
compressionFolder(filesFile, zipOutputStream, path);
} else {
compressionFile(filesFile, zipOutputStream, path);
}
} // for
} catch (IOException e) {
e.printStackTrace();
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。