标签:compress NPU std rcfile getpath tor direct ESS 文件的
文件压缩和解压
import java.io.*; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class FileZipOrUnZip { /** * @param sourceFullPath 源文件全路径 * @param zipFullPath 压缩后的全路径 * 压缩文件 * @throws Exception */ public static void zip(String sourceFullPath, String zipFullPath) throws Exception{ System.out.println("压缩中..."); ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFullPath)); File sourceFile = new File(sourceFullPath); compress(out,sourceFile,sourceFile.getName()); out.close(); System.out.println("压缩完成"); } public static void compress(ZipOutputStream out, File sourceFile,String base) throws Exception{ if (!sourceFile.isDirectory()) { out.putNextEntry( new ZipEntry(base) ); FileInputStream fos = new FileInputStream(sourceFile); BufferedInputStream bis = new BufferedInputStream(fos); int tag; System.out.println(base); while((tag=bis.read())!=-1){ out.write(tag); } bis.close(); fos.close(); return; } File[] flist = sourceFile.listFiles(); if(flist.length == 0){ System.out.println(base+"/"); out.putNextEntry(new ZipEntry(base+"/") ); return; } for(int i=0;i<flist.length;i++){ compress(out,flist[i],base+"/"+flist[i].getName()); } } /** * zip解压 * @param srcFile zip源文件 * @param destDirPath 解压后的目标文件夹 */ public static void unZip(File srcFile, String destDirPath) throws RuntimeException { long start = System.currentTimeMillis(); // 判断源文件是否存在 if (!srcFile.exists()) { throw new RuntimeException(srcFile.getPath() + "所指文件不存在"); } // 开始解压 ZipFile zipFile = null; try { zipFile = new ZipFile(srcFile); Enumeration<?> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); System.out.println("解压" + entry.getName()); // 如果是文件夹,就创建个文件夹 if (entry.isDirectory()) { String dirPath = destDirPath + "/" + entry.getName(); File dir = new File(dirPath); dir.mkdirs(); } else { // 如果是文件,就先创建一个文件,然后用io流把内容copy过去 File targetFile = new File(destDirPath + "/" + entry.getName()); // 保证这个文件的父文件夹必须要存在 if(!targetFile.getParentFile().exists()){ targetFile.getParentFile().mkdirs(); } targetFile.createNewFile(); // 将压缩文件内容写入到这个文件中 InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(targetFile); int len; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); is.close(); } } long end = System.currentTimeMillis(); System.out.println("解压完成,耗时:" + (end - start) +" ms"); } catch (Exception e) { throw new RuntimeException("unzip error from ZipUtils", e); } finally { if(zipFile != null){ try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws Exception { // zip("D:\git_test\test", "D:\git_test\test\\test1.zip"); unZip(new File("D:\\git_test\\test\\test1.zip"), "D:\\git_test\\test"); } }
标签:compress NPU std rcfile getpath tor direct ESS 文件的
原文地址:https://www.cnblogs.com/zengqinghong/p/12003197.html