首页 > 代码库 > apache ant解压zip。支持多级目录解压
apache ant解压zip。支持多级目录解压
package cn.liuc.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; public class ZipByOriginalExtractUtil { public static ArrayList<String> unzipFile(String zipFilePath) { String path = zipFilePath.replaceAll("\\\\", "/"); String outputDirectory = path.substring(0, path.lastIndexOf("."))+"/"; return unZip(zipFilePath, outputDirectory); } /** * 生存目录 * * @param directory * 解压文件存放目录 * @param subDirectory * 子目录(没有时可传入空字符串) */ private static 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 e) { e.printStackTrace(); } } /** * 解压指定的ZIP文件 * * @param unZipFileName * 文件名字符串(包含路径) * @param outputDirectory * 解压后存放目录 * @return */ public static ArrayList<String> unZip(String unZipFileName, String outputDirectory) { ArrayList<String> unzipFileList = new ArrayList<String>(); int readedBytes; FileOutputStream fileOutStream=null; byte[] buff = new byte[1024]; // File file; InputStream inputStream=null; ZipFile zipFile = null; try { createDirectory(outputDirectory, ""); if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) { zipFile = new ZipFile(unZipFileName, "GBK"); } else if (System.getProperty("os.name").toLowerCase() .indexOf("linux") >= 0) { zipFile = new ZipFile(unZipFileName, "UTF-8"); } for (Enumeration<ZipEntry> entries = zipFile.getEntries(); entries .hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { // 是目录,则创建之 String name = entry.getName().substring(0, entry.getName().length() - 1); File f = new File(outputDirectory + File.separator + name); f.mkdir(); // file.mkdirs(); } else { // 是文件 String fileName = entry.getName().replaceAll("\\\\", "/"); String path = outputDirectory + "/" + fileName; File f = new File(path); f.createNewFile(); inputStream = zipFile.getInputStream(entry); fileOutStream = new FileOutputStream(f); while ((readedBytes = inputStream.read(buff)) > 0) { fileOutStream.write(buff, 0, readedBytes); } fileOutStream.close(); inputStream.close(); unzipFileList.add(path); } } zipFile.close(); } catch (Exception e) { e.printStackTrace(); }finally{ if (inputStream!=null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutStream!=null) { try { fileOutStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return unzipFileList; } /** * * \brief 测试 * * @param args * @throws Exception * @attention 方法的使用注意事项 * @author liuchao * @date 2013-9-13 * @note begin modify by 修改人 修改时间 修改内容摘要说明 */ public static void main(String[] args) throws Exception { unzipFile("E:\\专家协同解决方案.zip"); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。