java工具类解压缩zip和rar

短命女 2023-05-28 09:49 77阅读 0赞

解压缩java工具类

  1. import com.github.junrar.Archive;
  2. import com.github.junrar.rarfile.FileHeader;
  3. import com.ramostear.unaboot.common.UnaBootConst;
  4. import org.apache.tools.zip.ZipEntry;
  5. import org.apache.tools.zip.ZipFile;
  6. import java.io.*;
  7. import java.util.Enumeration;
  8. /**
  9. * 压缩包工具类
  10. */
  11. public class ArchiveUtils {
  12. public static void unzip(String source,String target) throws IOException {
  13. unzip(new File(source),target);
  14. }
  15. public static void unzip(File source,String target) throws IOException {
  16. InputStream inputStream = null;
  17. OutputStream outputStream = null;
  18. File targetFile = new File(target);
  19. if(!targetFile.exists()){
  20. targetFile.mkdirs();
  21. }
  22. ZipFile zipFile = new ZipFile(source);
  23. for(Enumeration entries = zipFile.getEntries();entries.hasMoreElements();){
  24. ZipEntry zipEntry = (ZipEntry) entries.nextElement();
  25. String entryName = zipEntry.getName();
  26. inputStream = zipFile.getInputStream(zipEntry);
  27. String outPath = (target+"/"+entryName).replaceAll("\\*","/");
  28. File file = new File(outPath.substring(0,outPath.lastIndexOf("/")));
  29. if(!file.exists()){
  30. file.mkdirs();
  31. }
  32. if(new File(outPath).isDirectory()){
  33. continue;
  34. }
  35. outputStream = new FileOutputStream(outPath);
  36. byte[] bytes = new byte[1024];
  37. int length;
  38. while ((length = inputStream.read(bytes))>0){
  39. outputStream.write(bytes,0,length);
  40. }
  41. }
  42. outputStream.flush();
  43. outputStream.close();
  44. inputStream.close();
  45. zipFile.close();
  46. System.gc();
  47. }
  48. public static void unRar(String source,String target) throws Exception{
  49. File targetFile = new File(target);
  50. if(!targetFile.exists()){
  51. targetFile.mkdirs();
  52. }
  53. Archive archive = new Archive(new File(source));
  54. if(archive != null){
  55. FileHeader header = archive.nextFileHeader();
  56. while(header != null){
  57. if(header.isDirectory()){
  58. File file = new File(target+ File.separator+header.getFileNameString());
  59. file.mkdirs();
  60. }else{
  61. File file = new File(target+File.separator+header.getFileNameString());
  62. try{
  63. if(!file.exists()){
  64. if(!file.getParentFile().exists()){
  65. file.getParentFile().mkdirs();
  66. }
  67. file.createNewFile();
  68. }
  69. FileOutputStream outputStream = new FileOutputStream(file);
  70. archive.extractFile(header,outputStream);
  71. outputStream.close();
  72. }catch (Exception ex){
  73. ex.printStackTrace();
  74. }
  75. }
  76. header = archive.nextFileHeader();
  77. }
  78. }
  79. archive.close();
  80. }
  81. public static void main(String[] args) throws Exception {
  82. //unRar("F:/web.rar", "F:/test"); 注意需要是rar4压缩
  83. unzip("F:/web.zip", "F:/test");
  84. }
  85. }

依赖

  1. <dependency>
  2. <groupId>com.github.junrar</groupId>
  3. <artifactId>junrar</artifactId>
  4. <version>1.0.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>ant</groupId>
  8. <artifactId>ant</artifactId>
  9. <version>1.6.5</version>
  10. </dependency>

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,77人围观)

还没有评论,来说两句吧...

相关阅读