赞
踩
解压缩java工具类
import com.github.junrar.Archive; import com.github.junrar.rarfile.FileHeader; import com.ramostear.unaboot.common.UnaBootConst; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import java.io.*; import java.util.Enumeration; /** * 压缩包工具类 */ public class ArchiveUtils { public static void unzip(String source,String target) throws IOException { unzip(new File(source),target); } public static void unzip(File source,String target) throws IOException { InputStream inputStream = null; OutputStream outputStream = null; File targetFile = new File(target); if(!targetFile.exists()){ targetFile.mkdirs(); } ZipFile zipFile = new ZipFile(source); for(Enumeration entries = zipFile.getEntries();entries.hasMoreElements();){ ZipEntry zipEntry = (ZipEntry) entries.nextElement(); String entryName = zipEntry.getName(); inputStream = zipFile.getInputStream(zipEntry); String outPath = (target+"/"+entryName).replaceAll("\\*","/"); File file = new File(outPath.substring(0,outPath.lastIndexOf("/"))); if(!file.exists()){ file.mkdirs(); } if(new File(outPath).isDirectory()){ continue; } outputStream = new FileOutputStream(outPath); byte[] bytes = new byte[1024]; int length; while ((length = inputStream.read(bytes))>0){ outputStream.write(bytes,0,length); } } outputStream.flush(); outputStream.close(); inputStream.close(); zipFile.close(); System.gc(); } public static void unRar(String source,String target) throws Exception{ File targetFile = new File(target); if(!targetFile.exists()){ targetFile.mkdirs(); } Archive archive = new Archive(new File(source)); if(archive != null){ FileHeader header = archive.nextFileHeader(); while(header != null){ if(header.isDirectory()){ File file = new File(target+ File.separator+header.getFileNameString()); file.mkdirs(); }else{ File file = new File(target+File.separator+header.getFileNameString()); try{ if(!file.exists()){ if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } file.createNewFile(); } FileOutputStream outputStream = new FileOutputStream(file); archive.extractFile(header,outputStream); outputStream.close(); }catch (Exception ex){ ex.printStackTrace(); } } header = archive.nextFileHeader(); } } archive.close(); } public static void main(String[] args) throws Exception { //unRar("F:/web.rar", "F:/test"); 注意需要是rar4压缩 unzip("F:/web.zip", "F:/test"); } }
依赖
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.6.5</version>
</dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。