下文笔者讲述java代码解压zip和rar文件的方法分享,如下所示:
解压rar和解压zip的方法
实现思路: 解压rar文件使用junrar.jar即可 解压zip文件使用apache的ant.jar即可实现zip文件的解压例:
解压rar和解压zip的方法
1.引入jar包 <!-- 解压rar --> <dependency> <groupId>com.github.junrar</groupId> <artifactId>junrar</artifactId> <version>3.0.0</version> </dependency> <!-- 解压zip --> <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.9.4</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> 2.编写处理压缩文件的类 import com.github.junrar.Archive; import com.github.junrar.rarfile.FileHeader; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import java.io.*; import java.util.Enumeration; public class UnZipAnRar { public static void unZip(File zipFile, String outDir) throws IOException { File outFileDir = new File(outDir); if (!outFileDir.exists()) { boolean isMakDir = outFileDir.mkdirs(); if (isMakDir) { System.out.println("创建压缩目录成功"); } } ZipFile zip = new ZipFile(zipFile); for (Enumeration enumeration = zip.getEntries(); enumeration.hasMoreElements(); ) { ZipEntry entry = (ZipEntry) enumeration.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); if (entry.isDirectory()) { //处理压缩文件包含文件夹的情况 File fileDir = new File(outDir + zipEntryName); fileDir.mkdir(); continue; } File file = new File(outDir, zipEntryName); file.createNewFile(); OutputStream out = new FileOutputStream(file); byte[] buff = new byte[1024]; int len; while ((len = in.read(buff)) > 0) { out.write(buff, 0, len); } in.close(); out.close(); } } public static void unRar(File rarFile, String outDir) throws Exception { File outFileDir = new File(outDir); if (!outFileDir.exists()) { boolean isMakDir = outFileDir.mkdirs(); if (isMakDir) { System.out.println("创建压缩目录成功"); } } Archive archive = new Archive(new FileInputStream(rarFile)); FileHeader fileHeader = archive.nextFileHeader(); while (fileHeader != null) { if (fileHeader.isDirectory()) { fileHeader = archive.nextFileHeader(); continue; } File out = new File(outDir + fileHeader.getFileNameString()); if (!out.exists()) { if (!out.getParentFile().exists()) { out.getParentFile().mkdirs(); } out.createNewFile(); } FileOutputStream os = new FileOutputStream(out); archive.extractFile(fileHeader, os); os.close(); fileHeader = archive.nextFileHeader(); } archive.close(); } } 3.测试代码 import org.junit.Test; import java.io.File; import java.io.IOException; public class UnZipAnRarTest { @Test public void getUnZipAnnRarTest() { String originDir = "D:\\Test\\TestFile\\"; String zipPath = originDir + "test.zip"; File zipFile = new File(zipPath); String rarPath = originDir + "new.rar"; File rarFile = new File(rarPath); try { UnZipAnRar.unZip(zipFile, "D:\\Test\\TestFile\\Test\\"); } catch (IOException e) { e.printStackTrace(); } try { UnZipAnRar.unRar(rarFile, "D:\\Test\\TestFile\\测试\\"); } catch (Exception e) { e.printStackTrace(); } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。