java打包压缩文件
package com.it.simple.util;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Enumeration;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
/***
* 将压缩文件写出页面web下载
* @架包 commons-compress-1.8.jar
*
*/
public class ZipFileUtils{
public static void main(String[] args) throws FileNotFoundException, Exception {
/*ZipFileUtils main = new ZipFileUtils("c:/test1.zip");
main.createZipOut();
main.packToolFiles("D:/444", "444");
main.closeZipOut();*/
/*main = new Main("c:/test2.zip");
main.createZipOut();
main.packToolFiles("C:/test", "test");
main.closeZipOut();
Main.unZipToFolder("c:/test1.zip", "c:/test1");
Main.unZipToFolder("c:/test2.zip", "c:/test2");
*/
}
private OutputStream out = null;
private BufferedOutputStream bos = null;
private ZipArchiveOutputStream zaos = null;
private String zipFileName = null;
public ZipFileUtils(String zipname,HttpServletResponse response) {
this.zipFileName = zipname;
try{
String fileName = "文件打包下载_"+System.currentTimeMillis()+".zip";
response.setHeader("content-disposition", "attachment;filename=\""
+ new String(fileName.getBytes("gb2312"), "iso8859-1") + "\"");
//response.setContentType("APPLICATION/octet-stream");
response.setContentType("multipart/form-data");
out = response.getOutputStream();
}catch(Exception e){
}
}
public void createZipOut() throws FileNotFoundException, IOException {
// File f = new File(zipFileName);
// out = new FileOutputStream(f);
bos = new BufferedOutputStream(out);
zaos = new ZipArchiveOutputStream(bos);
zaos.setEncoding("GBK");
}
public void closeZipOut() throws Exception {
zaos.flush();
zaos.close();
bos.flush();
bos.close();
out.flush();
out.close();
}
/**
* 把一个目录打包到zip文件中的某目录
* @param dirpath 目录绝对地址
* @param pathName zip中目录
*/
public void packToolFiles(String dirpath, String pathName) throws FileNotFoundException, IOException {
if (StringUtils.isNotEmpty(pathName)) {
pathName = pathName + File.separator;
}
packToolFiles(zaos, dirpath, pathName);
}
/**
* 把一个目录打包到一个指定的zip文件中
* @param dirpath 目录绝对地址
* @param pathName zip文件抽象地址
*/
public void packToolFiles(ZipArchiveOutputStream zaos, String dirpath, String pathName) throws FileNotFoundException, IOException {
ByteArrayOutputStream tempbaos = new ByteArrayOutputStream();
BufferedOutputStream tempbos = new BufferedOutputStream(tempbaos);
File dir = new File(dirpath);
//返回此绝对路径下的文件
File[] files = dir.listFiles();
if (files == null || files.length < 1) {
return;
}
for (int i = 0; i < files.length; i++) {
//判断此文件是否是一个文件夹
if (files[i].isDirectory()) {
packToolFiles(zaos, files[i].getAbsolutePath(), pathName + files[i].getName() + File.separator);
} else {
zaos.putArchiveEntry(new ZipArchiveEntry(pathName + files[i].getName()));
IOUtils.copy(new FileInputStream(files[i].getAbsolutePath()), zaos);
zaos.closeArchiveEntry();
}
}
tempbaos.flush();
tempbaos.close();
tempbos.flush();
tempbos.close();
}
/**
* 把一个zip文件解压到一个指定的目录中
* @param zipfilename zip文件抽象地址
* @param outputdir 目录绝对地址
*/
public static void unZipToFolder(String zipfilename, String outputdir) throws IOException {
File zipfile = new File(zipfilename);
if (zipfile.exists()) {
outputdir = outputdir + File.separator;
FileUtils.forceMkdir(new File(outputdir));
ZipFile zf = new ZipFile(zipfile, "GBK");
Enumeration zipArchiveEntrys = zf.getEntries();
while (zipArchiveEntrys.hasMoreElements()) {
ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) zipArchiveEntrys.nextElement();
if (zipArchiveEntry.isDirectory()) {
FileUtils.forceMkdir(new File(outputdir + zipArchiveEntry.getName() + File.separator));
} else {
IOUtils.copy(zf.getInputStream(zipArchiveEntry), FileUtils.openOutputStream(new File(outputdir + zipArchiveEntry.getName())));
}
}
} else {
throw new IOException("指定的解压文件不存在:\t" + zipfilename);
}
}
}
注释:如果是web项目传outputStream流进来
java打包压缩文件的更多相关文章
- java ZIP压缩文件
问题描述: 使用java ZIP压缩文件和目录 问题解决: (1)单个文件压缩 注: 以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...
- java生成压缩文件
在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...
- Java生成压缩文件(zip、rar 格式)
jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...
- java ZipOutputStream压缩文件,ZipInputStream解压缩
java中实现zip的压缩与解压缩.java自带的 能实现的功能比较有限. 本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理). 对中文的支持需要使 ...
- Java实现压缩文件与解压缩文件
由于工作需要,需要将zip的压缩文件进行解压,经过调查发现,存在两个开源的工具包,一个是Apache的ant工具包,另一个就是Java api自带的工具包:但是Java自带的工具包存在问题:如果压缩或 ...
- CentOS 打包压缩文件 zip 命令详解
我们再linux中常见的压缩文件有.tar.gz,.zip,.gz,在linux中,你要习惯没有.rar的日子. 一下为tar,zip命令详解 tar -zcvf /home/files.tar.gz ...
- Java打包多文件成zip
package com.zh.java.util; import lombok.extern.slf4j.Slf4j; import java.io.File;import java.io.FileI ...
- Linux就该这么学--命令集合6(打包压缩文件、文件查询搜索命令)
1.tar命令用于对文件打包压缩或解压:(tar [选项] [文件]) 打包并压缩文件:tar -czvf 压缩包名.tar.gz 文件名 解压并展开压缩包:tar -xzvf 压缩包名.tar.gz ...
- java对压缩文件进行加密,winrar和好压 直接输入解密密码来使用
<!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j --> <dependency> <gro ...
随机推荐
- SpringMvc中的反射
controller中的方法,是通过反射调用的 spring监控controller中的注解,当命令符合某个注解的时候,通过反射,找到这个注解对应的方法,然后调用,处理完成得到返回值,再根据这个返回值 ...
- ulink 固件更新问题
1前言 ulink版本升级.. 当使用keil4.7.4版本时,ulink固件库更新了. 2官方资料解释: Boot Mode Utility Home » Appendix » Utilities ...
- Ogre中TerrainSceneManager
转自:http://blog.csdn.net/yanonsoftware/article/details/1103665 TerrainSceneManager是一个OctreeSceneManag ...
- 获得servletContext的路径的方法
request.getSession().getServletContext().getRealPath("")或 System.getProperty("webapp. ...
- python随笔
1. 使用iter实现接收用户多行输入 stopword = '' str = '' print('请将要添加的内容输入下方,输入空白行按回车退出程序:') for line in iter(inpu ...
- Osmocom-BB MOTO C118硬刷
写在最前面,先知我YY下硬刷最好可能实现的功能: 1.把软件刷入flash,修改loader后,可以实现上电就自动运行程序: 2.硬刷后,程序自动起来,可以修改loader就行加密 3.硬刷后,有可能 ...
- JQuery源码分析(七)
了解jQuery对DOM进行遍历背后的工作机制,这样可以在编写代码时有意识地避免一些不必要的重复操作,从而提升代码的性能. 关于jQuery对象的包装 var $aaron = $("aar ...
- 11g_job执行带参数的procedure
现有一个参数为date类型的存储: create or replace procedure t_pro(t_var in date) as begin dbms_output.put_line(T ...
- Magento在IE下登陆不了后台,在Firefox下正常
目前的解决办法如下: 方法一,用FF登陆后台,在 System—Configuration-Web-Session Cookie management....timeout 改为:86400 方法二: ...
- beta 阶段的 postmortem 报告
part一: 用户数:目前约30人 总结:与当初的预算差不多,首先1.第一次开发软件,经验效率不足,那是很正常的事情.2.用户数量少,因为宣传力度还是比较的少.应该加强软件的推广才行. part二: ...