java中压缩文件:
记录一个压缩文件的文章,希望能帮到大家直接就是一个工具类,可以直接用大家可以复制后先在main里面调用试试看!老样子直接上代码。
public class ZipCompressor {
static final int BUFFER = 8192;
private File zipFile;
public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
public void compress(String... pathName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
for (int i = 0; i < pathName.length; i++) {
compress(new File(pathName[i]), out, basedir);
}
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists()) {
throw new RuntimeException(srcPathName + "不存在!");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
if (file.isDirectory()) {
System.out.println("压缩:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("压缩:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists()) {
return;
}
File[] files = dir.listFiles();
if (files != null) {
if (files.length == 0) {
try {
out.putNextEntry(new ZipEntry(basedir + dir.getName() + File.separator));
} catch (Exception e) {
e.printStackTrace();
}
} else {
for (int i = 0; i < files.length; i++) {
compress(files[i], out, basedir + dir.getName() + "/");
}
}
}
}
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
try {
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/x-zip-compressed");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
}
觉得可以给个赞哦,大家一块学习哦记录生活,记录技术,这里是王不正的博客,啧啧啧啧啧!!!!!