• Ant压缩与解压缩


    package com.test.utils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.util.Enumeration;
    
    import org.apache.tools.ant.Project;
    import org.apache.tools.ant.taskdefs.Zip;
    import org.apache.tools.ant.types.FileSet;
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipFile;
    
    public class ZipFileHandler {
    
    	private final int buffer = 1024;
    
    	private void createDirectory(String directory, String subDirectory) {
    		String dir[];
    		File fl = new File(directory);
    		try {
    			if (subDirectory == "" && fl.exists() != true) {
    				fl.mkdir();
    			} else if (subDirectory != "") {
    				dir = subDirectory.replace('\', '/').split("/");
    				for (int i = 0; i < dir.length; i++) {
    					File subFile = new File(directory + File.separator + dir[i]);
    					if (subFile.exists() == false) {
    						subFile.mkdir();
    					}
    					directory += File.separator + dir[i];
    				}
    			}
    		} catch (Exception ex) {
    			System.out.println(ex.getMessage());
    		}
    	}
    
    	@SuppressWarnings("unchecked")
    	public boolean unZip(String zipFilePath, String outputDirectory) {
    		boolean flag = false;
    		try {
    			ZipFile zipFile = new ZipFile(zipFilePath);
    			Enumeration e = zipFile.getEntries();
    			ZipEntry zipEntry = null;
    			createDirectory(outputDirectory, "");
    			while (e.hasMoreElements()) {
    				zipEntry = (ZipEntry) e.nextElement();
    				System.out.println("unzip " + zipEntry.getName());
    				if (zipEntry.isDirectory()) {
    					String name = zipEntry.getName().trim();
    					name = name.substring(0, name.length() - 1);
    					File f = new File(outputDirectory + File.separator + name);
    					if (!f.exists()) {
    						f.mkdir();
    					}
    
    				} else {
    					String fileName = zipEntry.getName();
    					fileName = fileName.replace('\', '/');
    					if (fileName.indexOf("/") != -1) {
    						createDirectory(outputDirectory, fileName.substring(0,
    								fileName.lastIndexOf("/")));
    						fileName = fileName
    								.substring(fileName.lastIndexOf("/") + 1);
    					}
    					File f = new File(outputDirectory + File.separator
    							+ zipEntry.getName());
    					f.createNewFile();
    					InputStream in = zipFile.getInputStream(zipEntry);
    					FileOutputStream out = new FileOutputStream(f);
    					byte[] by = new byte[buffer];
    					int c;
    					while ((c = in.read(by)) != -1) {
    						out.write(by, 0, c);
    					}
    					in.close();
    					out.close();
    				}
    			}
    			flag = true;
    			System.out.println("unzip finished");
    		} catch (Exception ex) {
    			System.out.println(ex.getMessage());
    		}		
    		return flag;
    	}
    
    	public boolean zip(String srcDirName, String zipFilePath) {
    		boolean flag = false;
    		try {
    			File srcdir = new File(srcDirName);
    			if (!srcdir.exists())
    				throw new RuntimeException(srcDirName + " is not exist!");
    
    			Project prj = new Project();
    			Zip zip_ = new Zip();
    			zip_.setProject(prj);
    			zip_.setDestFile(new File(zipFilePath));
    
    			FileSet fileSet = new FileSet();
    			fileSet.setProject(prj);
    			fileSet.setDir(srcdir);
    			zip_.addFileset(fileSet);
    
    			zip_.execute();
    			flag = true;
    			System.out.println("zip finished");
    		} catch (Exception ex) {
    			System.out.println(ex.getMessage());
    		}
    		return flag;
    	}
    
    	public static void main(String[] args) {
    		ZipFileHandler zh = new ZipFileHandler();
    		zh.unZip("C:\test.zip", "C:\test1");
    		zh.zip("C:\test1", "C:\test1.zip");
    	}
    
    }
    
  • 相关阅读:
    POJ 1703 Find them, Catch them (数据结构-并查集)
    ProductHunt:创业公司产品猎场和秀场
    firedac数据集控件的公共祖先类——TFDAdaptedDataSet
    IDFTP连不上FTP服务器的解决方法
    SQLServer到底支持多少连接数的并发?
    日志
    权限
    DATASNAP中间件调用带OUTPUT参数的存储过程
    连接池中的连接超过数据库端的最大空闲时间
    firedac的TFDStoredProc动态创建并调用存储过程
  • 原文地址:https://www.cnblogs.com/zhangfei/p/3296627.html
Copyright © 2020-2023  润新知