Java压缩和解压文件工具类ZipUtil
来源:互联网 发布:快速瘦大腿 知乎 编辑:程序博客网 时间:2024/05/20 06:36
- </pre><pre code_snippet_id="1837335" snippet_file_name="blog_20160818_2_1853501" name="code" class="java">packagecom.utility.zip;
-
- importjava.io.BufferedInputStream;
- importjava.io.BufferedOutputStream;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.util.zip.ZipEntry;
- importjava.util.zip.ZipInputStream;
- importjava.util.zip.ZipOutputStream;
-
- importcom.utility.io.IOUtil;
-
-
-
-
-
-
-
- publicfinal class ZipUtil {
-
- privateZipUtil() {
-
- }
-
-
-
-
-
-
-
-
- publicstatic File zip(String filePath) {
- File target = null;
- File source = newFile(filePath);
- if(source.exists()) {
-
- String zipName = source.getName() + ".zip";
- target = newFile(source.getParent(), zipName);
- if(target.exists()) {
- target.delete();
- }
- FileOutputStream fos = null;
- ZipOutputStream zos = null;
- try{
- fos = newFileOutputStream(target);
- zos = newZipOutputStream(newBufferedOutputStream(fos));
-
- addEntry("/", source, zos);
- }catch(IOException e) {
- thrownew RuntimeException(e);
- }finally{
- IOUtil.closeQuietly(zos, fos);
- }
- }
- returntarget;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- privatestatic void addEntry(String base, File source, ZipOutputStream zos)
- throwsIOException {
-
- String entry = base + source.getName();
- if(source.isDirectory()) {
- for(File file : source.listFiles()) {
-
- addEntry(entry + "/", file, zos);
- }
- }else{
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try{
- byte[] buffer = newbyte[1024* 10];
- fis = newFileInputStream(source);
- bis = newBufferedInputStream(fis, buffer.length);
- intread = 0;
- zos.putNextEntry(newZipEntry(entry));
- while((read = bis.read(buffer, 0, buffer.length)) != -1) {
- zos.write(buffer,0, read);
- }
- zos.closeEntry();
- }finally{
- IOUtil.closeQuietly(bis, fis);
- }
- }
- }
-
-
-
-
-
-
-
- publicstatic void unzip(String filePath) {
- File source = newFile(filePath);
- if(source.exists()) {
- ZipInputStream zis = null;
- BufferedOutputStream bos = null;
- try{
- zis = newZipInputStream(newFileInputStream(source));
- ZipEntry entry = null;
- while((entry = zis.getNextEntry()) != null
- && !entry.isDirectory()) {
- File target = newFile(source.getParent(), entry.getName());
- if(!target.getParentFile().exists()) {
-
- target.getParentFile().mkdirs();
- }
-
- bos = newBufferedOutputStream(newFileOutputStream(target));
- intread = 0;
- byte[] buffer = newbyte[1024* 10];
- while((read = zis.read(buffer, 0, buffer.length)) != -1) {
- bos.write(buffer,0, read);
- }
- bos.flush();
- }
- zis.closeEntry();
- }catch(IOException e) {
- thrownew RuntimeException(e);
- }finally{
- IOUtil.closeQuietly(zis, bos);
- }
- }
- }
-
- publicstatic void main(String[] args) {
- String targetPath = "E:\\Win7壁纸";
- File file = ZipUtil.zip(targetPath);
- System.out.println(file);
- ZipUtil.unzip("F:\\Win7壁纸.zip");
- }
- }
-
- packagecom.utility.io;
-
- importjava.io.Closeable;
- importjava.io.IOException;
-
-
-
-
-
-
-
- publicclass IOUtil {
-
-
-
-
-
-
-
- publicstatic void close(Closeable... closeables) throwsIOException {
- if(closeables != null) {
- for(Closeable closeable : closeables) {
- if(closeable != null) {
- closeable.close();
- }
- }
- }
- }
-
-
-
-
-
-
-
- publicstatic void closeQuietly(Closeable... closeables) {
- try{
- close(closeables);
- }catch(IOException e) {
-
- }
- }
-
- }
用于服务器端打包文件的,将压缩后的文件写入到response输出流即可实现在服务器端打包下载,支持多级目录嵌套。测试时可以先用ZipUtil.zip压缩某个文件夹test,得到压缩文件test.zip,然后将文件夹test删除,用ZipUtil.unzip解压test.zip即可还原。PS:需要解决中文乱码的朋友可以参考此处 http://log-cd.iteye.com/blog/585647