压缩工具类

来源:互联网 发布:淘宝大米 编辑:程序博客网 时间:2024/05/21 16:13
package com.*.util;import java.io.BufferedOutputStream;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 org.apache.tools.zip.*;public class ZipToolsUtil {private static ZipFile zipFile = null;private static ZipOutputStream zipOut = null; // 压缩Zip//private static ZipEntry zipEntry = null;@SuppressWarnings("unused")private static int bufSize = 0; // size of bytesprivate static byte[] buf = new byte[1024 * 10];private static int readedBytes = 0;public ZipToolsUtil() {this(512);}public ZipToolsUtil(int bufSize) {ZipToolsUtil.bufSize = bufSize;buf = new byte[bufSize];}// 压缩文件夹内的文件public static String doZip(File zipDir, int flag) {// zipDirectoryPath:需要压缩的文件夹名    System.out.println("ZipDir is:"+zipDir.toString());String zipFileName = ZipToolsUtil.JoinPath(zipDir.getParent(), zipDir.getName()+".zip");// 压缩后生成的zip文件名System.out.println("ZipFileName is:"+zipFileName);try {zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));handleDir(zipDir, zipOut ,flag);zipOut.close();} catch (IOException ioe) {ioe.printStackTrace();}return zipFileName;}// 由doZip调用,递归完成目录文件读取@SuppressWarnings("static-access")private static void handleDir(File dir, ZipOutputStream zipOut, int flag) throws IOException {FileInputStream fileIn;File[] files;files = dir.listFiles();if (files.length == 0) {// 如果目录为空,则单独创建之.// ZipEntry的isDirectory()方法中,目录以"/"结尾.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));zipOut.closeEntry();} else {// 如果目录不为空,则分别处理目录和文件.for (File fileName : files) {// System.out.println(fileName);if (fileName.isDirectory()) {handleDir(fileName, zipOut, flag);} else {String zipFileName = fileName.toString();int index = zipFileName.lastIndexOf(fileName.separator);zipFileName = zipFileName.substring(index+1);int xml = zipFileName.indexOf(".");String xmlFile = zipFileName.substring(xml+1);if(flag ==1){if(xmlFile.compareToIgnoreCase("xml") == 0){continue;}zipOut.putNextEntry(new ZipEntry(zipFileName));fileIn = new FileInputStream(fileName);System.out.println("Adding zip file:" + fileName);while ((readedBytes = fileIn.read(buf)) > 0) {zipOut.write(buf, 0, readedBytes);}}else if(flag ==2){if(zipFileName.compareToIgnoreCase("publish.xml") == 0){zipOut.putNextEntry(new ZipEntry(zipFileName));fileIn = new FileInputStream(fileName);System.out.println("Adding zip file:" + fileName);while ((readedBytes = fileIn.read(buf)) > 0) {zipOut.write(buf, 0, readedBytes);}break;}}zipOut.closeEntry();}}}}// 解压指定zip文件@SuppressWarnings("rawtypes")public static void unZip(String unZipfileName, String targetPath) throws Exception {// unZipfileName需要解压的zip文件名FileOutputStream fileOut = null;File file = null;InputStream inputStream = null;File srcFile = new File(unZipfileName);String rootPath = "";if (srcFile.exists()) {if (targetPath == null || targetPath.isEmpty()) {rootPath = srcFile.getParent();} else {rootPath = targetPath;}} else {throw new Exception(unZipfileName + ",文件不存在");}try {zipFile = new ZipFile(unZipfileName);for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();) {ZipEntry entry = (ZipEntry) entries.nextElement();file = new File(rootPath + File.separator + entry.getName());if (entry.isDirectory()) {file.mkdirs();} else {// 如果指定文件的目录不存在,则创建之.File parent = file.getParentFile();if (!parent.exists()) {parent.mkdirs();}inputStream = zipFile.getInputStream(entry);fileOut = new FileOutputStream(file);while ((readedBytes = inputStream.read(buf)) > 0) {fileOut.write(buf, 0, readedBytes);}fileOut.close();inputStream.close();}}zipFile.close();} catch (IOException ioe) {ioe.printStackTrace();}}// 设置缓冲区大小public void setBufSize(int bufSize) {ZipToolsUtil.bufSize = bufSize;}    public static String JoinPath(String...paths){    if(paths.length == 0){    return "";    }    String path = paths[0];    for(int i = 1; i < paths.length;i++){    path += File.separator;    path += paths[i];    }    return path;    }// 测试AntZip类public static void main(String[] args) throws Exception {//ZipToolsUtil zip = new ZipToolsUtil();String dir = "D:/founder/test1/old_jsp.zip";//zip.doZip(new File(dir), 1);ZipToolsUtil.unZip(dir, "");//if (args.length == 2) {//String name = args[1];//ZipToolsUtil zip = new ZipToolsUtil();////if (args[0].equals("-zip"))//zip.doZip(name);//else if (args[0].equals("-unzip"))//zip.unZip(name);//} else {//System.out.println("Usage:");//System.out.println("压缩:java AntZip -zip directoryName");//System.out.println("解压:java AntZip -unzip fileName.zip");//throw new Exception("Arguments error!");//}}}

0 0
原创粉丝点击