java解压缩文件的实现示例,支持rar和zip格式 ____http://my.oschina.net/manville/blog/280420

本文介绍了解压ZIP和RAR文件的方法,使用了第三方库ant-1.6.5.jar和junrar-0.7.jar。提供了Java代码实现,包括压缩文件、解压ZIP文件以及解压RAR文件的具体步骤。

解压ZIP需要用到第三方jar包ant-1.6.5.jar,实际用到的是其中的zip下类。

解压RAR需要用用junrar-0.7.jar包。

这两个jar包的下载地址为:

CSDN下载页面

csdn没有积分的,可以去百度云网盘下载:

百度云下载页面

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
 
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
 
/**
  * <p>
  * Title: 解压缩文件
  * </p>
  * <p>
  * Copyright: Copyright (c) 2010
  * </p>
  * <p>
  * Company: yourcompany
  * </p>
  *
  * @author yourcompany
  * @version 1.0
  */
public class CompressFile {
     
 
     /**
     * 压缩文件
     *
     * @param srcfile
     *            File[] 需要压缩的文件列表
     * @param zipfile
     *            File 压缩后的文件
     */
     public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
         byte [] buf = new byte [ 1024 ];
         try {
             ZipOutputStream out = new ZipOutputStream( new FileOutputStream(
                     zipfile));
             for ( int i = 0 ; i < srcfile.length; i++) {
                 FileInputStream in = new FileInputStream(srcfile[i]);
                 out.putNextEntry( new ZipEntry(srcfile[i].getName()));
                 String str = srcfile[i].getName();
                 int len;
                 while ((len = in.read(buf)) > 0 ) {
                     out.write(buf, 0 , len);
                 }
                 out.closeEntry();
                 in.close();
             }
             out.close();
             System.out.println( "压缩完成." );
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
     /**
     * zip解压缩
     *
     * @param zipfile
     *            File 需要解压缩的文件
     * @param descDir
     *            String 解压后的目标目录
     */
     public static void unZipFiles(java.io.File zipfile, String descDir) {
         try {
             ZipFile zf = new ZipFile(zipfile);
             for (Enumeration entries = zf.getEntries(); entries
                     .hasMoreElements();) {
                 ZipEntry entry = ((ZipEntry) entries.nextElement());
                 String zipEntryName = entry.getName();
                 InputStream in = zf.getInputStream(entry);
                 OutputStream out = new FileOutputStream(descDir + zipEntryName);
                 byte [] buf1 = new byte [ 1024 ];
                 int len;
                 while ((len = in.read(buf1)) > 0 ) {
                     out.write(buf1, 0 , len);
                 }
                 in.close();
                 out.close();
                 //System.out.println("解压缩完成.");
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
     
 
     /**
     * 根据原始rar路径,解压到指定文件夹下.     
     * @param srcRarPath 原始rar路径
     * @param dstDirectoryPath 解压到的文件夹     
     */
     public static void unRarFile(String srcRarPath, String dstDirectoryPath) {
         if (!srcRarPath.toLowerCase().endsWith( ".rar" )) {
             System.out.println( "非rar文件!" );
             return ;
         }
         File dstDiretory = new File(dstDirectoryPath);
         if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
             dstDiretory.mkdirs();
         }
         Archive a = null ;
         try {
             a = new Archive( new File(srcRarPath));
             if (a != null ) {
                 a.getMainHeader().print(); // 打印文件信息.
                 FileHeader fh = a.nextFileHeader();
                 while (fh != null ) {
                     if (fh.isDirectory()) { // 文件夹
                         File fol = new File(dstDirectoryPath + File.separator
                                 + fh.getFileNameString());
                         fol.mkdirs();
                     } else { // 文件
                         File out = new File(dstDirectoryPath + File.separator
                                 + fh.getFileNameString().trim());
                         //System.out.println(out.getAbsolutePath());
                         try { // 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
                             if (!out.exists()) {
                                 if (!out.getParentFile().exists()) { // 相对路径可能多级,可能需要创建父目录.
                                     out.getParentFile().mkdirs();
                                 }
                                 out.createNewFile();
                             }
                             FileOutputStream os = new FileOutputStream(out);
                             a.extractFile(fh, os);
                             os.close();
                         } catch (Exception ex) {
                             ex.printStackTrace();
                         }
                     }
                     fh = a.nextFileHeader();
                 }
                 a.close();
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
     
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值