java实现的文件压缩和解压
来源:互联网 发布:仓库办公软件 编辑:程序博客网 时间:2024/04/30 01:10
java实现的文件压缩和解压
package com.cn;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipException;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;/** * @author * */public class ZipUtil { /** * 缓冲大小 */ private static int BUFFERSIZE = 2 << 10; /** * 压缩 * @param paths * @param fileName */ public static void zip(String[] paths, String fileName) { ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(fileName)); for(String filePath : paths) { //递归压缩文件 File file = new File(filePath); String relativePath = file.getName(); if(file.isDirectory()) { relativePath += File.separator; } zipFile(file, relativePath, zos); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(zos != null) { zos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void zipFile(File file, String relativePath, ZipOutputStream zos) { InputStream is = null; try { if(!file.isDirectory()) { ZipEntry zp = new ZipEntry(relativePath); zos.putNextEntry(zp); is = new FileInputStream(file); byte[] buffer = new byte[BUFFERSIZE]; int length = 0; while ((length = is.read(buffer)) >= 0) { zos.write(buffer, 0, length); } zos.flush(); zos.closeEntry(); } else { for(File f: file.listFiles()) { zipFile(f, relativePath + f.getName() + File.separator, zos); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if(is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 解压缩 * @param fileName * @param path */ public static void unzip(String fileName, String path) { FileOutputStream fos = null; InputStream is = null; try { ZipFile zf = new ZipFile(new File(fileName)); Enumeration en = zf.entries(); while (en.hasMoreElements()) { ZipEntry zn = (ZipEntry) en.nextElement(); if (!zn.isDirectory()) { is = zf.getInputStream(zn); File f = new File(path + zn.getName()); File file = f.getParentFile(); file.mkdirs(); fos = new FileOutputStream(path + zn.getName()); int len = 0; byte bufer[] = new byte[BUFFERSIZE]; while (-1 != (len = is.read(bufer))) { fos.write(bufer, 0, len); } fos.close(); } } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(null != is) { is.close(); } if(null != fos) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * @param args */ public static void main(String[] args) { zip(new String[] {"D:/Development/work/javaProject/src/com/cn/ZipUtil.java"}, "D:/Development/test.zip"); unzip("D:/Development/test.zip", "D:/Development/test/"); } }
原作者:http://songfeng-123.iteye.com/blog/2243016
0 0
- java实现的文件压缩和解压
- java中对文件解压和压缩的实现
- java程序实现对文件的压缩和解压
- Java实现Zip文件的解压和压缩_ZipUtil
- JAVA自己实现的文件压缩解压
- Java实现文件的压缩与解压
- Java实现文件的压缩与解压
- Java实现文件的压缩与解压
- Java文件的压缩和解压操作
- Java实现Gzip的压缩和解压
- java实现zip的压缩和解压
- java压缩和解压文件
- java实现Zip文件压缩和解压工程代码
- Java 实现*.tar.gz文件压缩和解压
- 文件的压缩和解压
- 文件的压缩和解压
- Java 实现zip格式的文件压缩与解压
- 用java代码实现文件的zip压缩与解压
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- 浏览器显示网站小图标
- 关于对erlang中由于没有return导致嵌套太深的解决办法
- Cloud Foundry 部署问题(1) deploy.yml 格式
- tomcat 在linux下权限配置
- java实现的文件压缩和解压
- oracle触发器(转载)
- IMP-00013: only a DBA can import a file exported by another DBA
- Android手机 Fildder真机抓包
- 线程同步
- HTML入门
- java学习之Calender类
- 获取手机里所有的app并点击相应的图标开启相应的程序
- MySQL 5.5 服务器变量详解