java压缩文件util
来源:互联网 发布:淘宝游戏交易平台官网 编辑:程序博客网 时间:2024/06/07 21:49
package cn.ipanel.apps.portalBackOffice.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.Enumeration;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;/** * * @create 2009-7-25 下午06:17:33 * @since */public class ZipUtil {public static final String FILE_SEPARATOR = System.getProperty("file.separator");/** * 将指定的文件解压缩到指定的文件夹,解压后的文件夹目录和给定的压缩文件名相同. * * @param zipFilePath * 全路径 * @param unZipDirectory * 全路径 * @return 解压缩文件是否成功. * @throws IOException */public static boolean unZipFile(String zipFilePath, String unZipDirectory)throws IOException {ZipFile zipFile = new ZipFile(zipFilePath);Enumeration<?> entries = zipFile.getEntries();if (zipFile == null) {return false;}while (entries.hasMoreElements()) {ZipEntry zipEntry = (ZipEntry) entries.nextElement();File f = new File(unZipDirectory + FILE_SEPARATOR+ zipEntry.getName());if (zipEntry.isDirectory()) {if (!f.exists() && !f.mkdirs())throw new IOException("Couldn't create directory: " + f);} else {BufferedInputStream is = null;BufferedOutputStream os = null;try {is = new BufferedInputStream(zipFile.getInputStream(zipEntry));File destDir = f.getParentFile();if (!destDir.exists() && !destDir.mkdirs()) {throw new IOException("Couldn't create dir " + destDir);}os = new BufferedOutputStream(new FileOutputStream(f));int b = -1;while ((b = is.read()) != -1) {os.write(b);}} finally {if (is != null)is.close();if (os != null)os.close();}}}zipFile.close();return true;}/** * 压缩一个文件 * @param filePath * @param zipPath * @return */public static boolean zipFile(String filePath,String zipPath){ BufferedReader in=null; org.apache.tools.zip.ZipOutputStream out=null; try{File file=new File(filePath); in=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"ISO-8859-1")); FileOutputStream f=new FileOutputStream(zipPath); CheckedOutputStream ch=new CheckedOutputStream(f,new CRC32()); out=new org.apache.tools.zip.ZipOutputStream(new BufferedOutputStream(ch)); int c; out.putNextEntry(new org.apache.tools.zip.ZipEntry(file.getName())); while((c=in.read())!=-1) out.write(c); } catch(Exception e){ e.printStackTrace(); return false; } finally{ try { if(in!=null) in.close(); if(out!=null) out.close();} catch (IOException e) {e.printStackTrace();} } return true;}/** * 压缩一个目录 * @param dir * @param zipPath * @return */public static boolean zipDirectory(String dir,String zipPath ){org.apache.tools.zip.ZipOutputStream out=null; try{File dirFile=new File(dir);if(!dirFile.isDirectory())return false;FileOutputStream fo=new FileOutputStream(zipPath); CheckedOutputStream ch=new CheckedOutputStream(fo,new CRC32()); out=new org.apache.tools.zip.ZipOutputStream(new BufferedOutputStream(ch)); zip(out,dirFile,""); } catch(Exception e){ e.printStackTrace(); return false; } finally{ try { if(out!=null) out.close(); } catch (IOException e) {e.printStackTrace();} } return true;}public static void zip(org.apache.tools.zip.ZipOutputStream out,File f,String base)throws Exception{//System.out.println("Zipping "+f.getName());if (f.isDirectory()) {File[] fl=f.listFiles();out.putNextEntry(new org.apache.tools.zip.ZipEntry(base+"/"));base=base.length()==0?"":base+"/";for (int i=0;i<fl.length ;i++ ){zip(out,fl[i],base+fl[i].getName());}}else {out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));FileInputStream is=new FileInputStream(f);BufferedInputStream in = new BufferedInputStream(is);//修改BUG!二进制输出采用bufferedint b;while ((b=in.read()) != -1)out.write(b);in.close();}} public static void main(String[] args){ // boolean f=zipFile("e:/red100.txt","e:/red.zip"); //boolean f=zipDirectory("e:/red","e:/red2.zip"); try {unZipFile("D:\\portal_rtb.zip", "D:\\wzb");//zipDirectory("F:\\list","F:\\list.zip");} catch (Exception e) {e.printStackTrace();} } }
阅读全文
0 0
- java压缩文件util
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- 用java中的java.util.zip包读取压缩文件
- 使用java.util.zip包压缩和解压缩文件
- 使用java.util.zip包压缩和解压缩文件
- util.ZipOutputStream 压缩文件
- Java压缩文件和解压缩文件
- vc++中压缩文件--ZIP Util
- java 压缩文件
- java压缩文件
- java压缩文件
- java 压缩文件
- java 压缩文件
- JAVA压缩文件
- java压缩文件
- JAVA 压缩文件
- java压缩文件
- java 压缩文件
- Python2.7:打印字符串是Unicode
- unity 边缘检测shader简介(一)
- 《数据库SQL实战》查找所有员工的last_name和first_name以及对应的dept_name
- NOIP2010普及组-接水问题
- java基础?T
- java压缩文件util
- jQuery+jQuery UI库的使用
- 怎样在浏览器中校验并修改密码
- lightoj 1027
- Java 运算符
- 51Nod
- [剑指offer]调整数组顺序使奇数位于偶数前面
- MySQL:有关联的两张表重新生成主键如何关联
- Nvigitiontion Drawer Activity的使用