zoukankan      html  css  js  c++  java
  • java 压缩和解压zip包

    网上有关压缩和解压zip包的博文一大堆,我随便找了一个。看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正:

    package com.wangpeng.utill;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    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 wangpeng
     */
    public class ToolOfZip {
    
    	/**
    	 * 解压zip包
    	 * @param inputPath 被解压的zip包的路径
    	 * @param targetPath 被解压到的文件夹
    	 * @param isClear 是否清楚已有文件夹
    	 */
    	public static void unZip(String inputPath,String targetPath, boolean isClear) {
    		try {
    			if (null == targetPath || "".equals(targetPath)) {
    				targetPath = inputPath.substring(0,inputPath.lastIndexOf("."));
    			} 
    
    			ZipFile zipFile = new ZipFile(new File(inputPath));
    			Enumeration<? extends ZipEntry> entrys = zipFile.entries();
    
    			while (entrys.hasMoreElements()) {
    				ZipEntry entry = entrys.nextElement();
    				String name = entry.getName();// aaa	estdirxxxxxeee.txt
    				String strTargetPath=targetPath+"/"+name;
    				String strTargetDir=strTargetPath.substring(0, strTargetPath.lastIndexOf(File.separator));
    				if (entry.isDirectory()) {
    					File dir = new File(strTargetPath);
    					if (!dir.exists()) {
    						dir.mkdirs();
    					}
    				} else {
    					File dir = new File(strTargetDir);
    					if (!dir.exists()) {
    						dir.mkdirs();
    					}
    					
    					File file = new File(strTargetPath);
    					if (file.exists() && isClear) {
    						file.delete();
    					}
    					
    					if (!file.exists()) {
    						InputStream in = zipFile.getInputStream(entry);
    						ToolOfFile.copyFile(in, file.getAbsolutePath());
    					}
    				}
    			}
    		} catch (ZipException ex) {
    			ex.printStackTrace();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * 创建ZIP文件
    	 * 
    	 * @param sourcePath
    	 *            文件或文件夹路径
    	 * @param zipPath
    	 *            生成的zip文件存在路径(包含文件名称)
    	 */
    	public static void createZip(String sourcePath, String zipPath) {
    		FileOutputStream fos = null;
    		ZipOutputStream zos = null;
    		try {
    			fos = new FileOutputStream(zipPath);
    			zos = new ZipOutputStream(fos);
    			writeZip(new File(sourcePath), "", zos);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (zos != null) {
    					zos.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private static void writeZip(File inFile, String parentPath,
    			ZipOutputStream zipOutStream) {
    		if (inFile.exists()) {
    			if (inFile.isDirectory()) {
    				parentPath += inFile.getName() + File.separator;
    				File[] files = inFile.listFiles();
    				for (File file : files) {
    					writeZip(file, parentPath, zipOutStream);
    				}
    			} else {
    				FileInputStream fileInStream = null;
    				try {
    					fileInStream = new FileInputStream(inFile);
    					ZipEntry entry = new ZipEntry(parentPath + inFile.getName());
    					zipOutStream.putNextEntry(entry);
    
    					byte[] buff = new byte[1024];
    					int len;
    					while ((len = fileInStream.read(buff)) != -1) {
    						zipOutStream.write(buff, 0, len);
    						zipOutStream.flush();
    					}
    
    				} catch (FileNotFoundException e) {
    					e.printStackTrace();
    				} catch (IOException e) {
    					e.printStackTrace();
    				} finally {
    					try {
    						if (fileInStream != null) {
    							fileInStream.close();
    						}
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		String oldPath = "F:/test/ddd/";
    		String newPath = "F:/test/ccc.zip";
    		// ToolOfZip.createZip(oldPath, newPath);
    		//ToolOfZip.unZip(newPath, null, false);
    		System.out.println();
    		System.out.println("---------ok----------");
    	}
    }
    


     

  • 相关阅读:
    Django异步与定时任务Celery
    SkyWalking 分布式追踪系统
    SharePoint 2010 硬件详细需求
    使用SharePoint 2010 客户端对象模型进行文档库及文档的操作
    SharePoint 2010 部署架构
    【git】项目添加.gitignore忽略.idea文件夹
    Android 横屏切换竖屏Activity的生命周期(转)
    经过完整测试的农历公历相互转换
    TZ ERP项目的随想
    C#加密与解密
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6789907.html
Copyright © 2011-2022 走看看