这两天,因为项目需要,涉及到文件夹的上传,想了很久,在网上也找了一些资料,都没有什么很好的办法,都是用插件之类的解决,后面就想到了上传压缩文件,说到压缩文件,自然就会遇到,文件的解压缩问题,大家都知道,比较常见的压缩文件有rar,zip,然而rar,zip的区别又在哪?说一点,zip 压缩算法是免费开放的,任何人可以免费使用。但是 RAR 就不一样了,
这个压缩算法已经受到专利权的保护,如果要使用 RAR 算法必须向其专利所有人支付费用。所以在一般的开源网站,像apache,sourceforge等开源网站上的开源项目一般都用zip格式.本人所学语言主要为java自然会想用java去解压,用java去解压zip比较容易,有apache提供的开源项目ant,我在网上找一下,找到了sourceforge的开源项目unrar专用于压缩,解压rar.只可惜没有文档(让人即喜,又悲).

     下面是一个对zip,rar进行解压的程序(从转载,供大家一起享用,另外希望对unrar有比较发解的朋友,能发表一个人见解),

    


Java代码



  1. import
  2. import
  3. import
  4. import
  5. import


import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


Java代码



  1. import


import de.innosystec.unrar.Archive;


Java代码


1. /**  
2.  *  *   
3.  * @version 创建时间:Feb 26, 2009 6:01:11 PM  
4.  * 类说明:压缩、解压文件公用类 
5.  * 
6.  */
7. public class
8. private static final int BUFFEREDSIZE = 1024;   
9.        
10. /** 
11.      * 解压zip格式的压缩文件到指定位置 
12.      * @param zipFileName 压缩文件 
13.      * @param extPlace 解压目录 
14.      * @throws Exception 
15.      */
16. @SuppressWarnings("unchecked")   
17. public synchronized void unzip(String zipFileName, String extPlace) throws
18. try
19. new
20. new
21. new
22. if((!f.exists()) && (f.length() <= 0)) {   
23. throw new Exception("要解压的文件不存在!");   
24.             }   
25.             String strPath, gbkPath, strtemp;   
26. new
27.             strPath = tempFile.getAbsolutePath();   
28.             java.util.Enumeration e = zipFile.getEntries();   
29. while(e.hasMoreElements()){   
30.                 org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();   
31.                 gbkPath=zipEnt.getName();   
32. if(zipEnt.isDirectory()){   
33.                     strtemp = strPath + File.separator + gbkPath;   
34. new
35.                     dir.mkdirs();   
36. continue;   
37. else
38. //读写文件 
39.                     InputStream is = zipFile.getInputStream(zipEnt);   
40. new
41.                     gbkPath=zipEnt.getName();   
42.                     strtemp = strPath + File.separator + gbkPath;   
43.                    
44. //建目录 
45.                     String strsubdir = gbkPath;   
46. for(int i = 0; i < strsubdir.length(); i++) {   
47. if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {   
48. 0, i);   
49. new
50. if(!subdir.exists())   
51.                             subdir.mkdir();   
52.                         }   
53.                     }   
54. new
55. new
56. int
57. while((c = bis.read()) != -1) {   
58. byte) c);   
59.                     }   
60.                     bos.close();   
61.                     fos.close();   
62.                 }   
63.             }   
64. catch(Exception e) {   
65.             e.printStackTrace();   
66. throw
67.         }   
68.     }   
69.        
70. /** 
71.      * 解压zip格式的压缩文件到指定位置 
72.      * @param zipFileName 压缩文件 
73.      * @param extPlace 解压目录 
74.      * @throws Exception 
75.      */
76. @SuppressWarnings("unchecked")   
77. public synchronized void unzip(String zipFileName, String extPlace,boolean whether) throws
78. try
79. new
80. new
81. new
82. if((!f.exists()) && (f.length() <= 0)) {   
83. throw new Exception("要解压的文件不存在!");   
84.             }   
85.             String strPath, gbkPath, strtemp;   
86. new
87.             strPath = tempFile.getAbsolutePath();   
88.             java.util.Enumeration e = zipFile.getEntries();   
89. while(e.hasMoreElements()){   
90.                 org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();   
91.                 gbkPath=zipEnt.getName();   
92. if(zipEnt.isDirectory()){   
93.                     strtemp = strPath + File.separator + gbkPath;   
94. new
95.                     dir.mkdirs();   
96. continue;   
97. else
98. //读写文件 
99.                     InputStream is = zipFile.getInputStream(zipEnt);   
100. new
101.                     gbkPath=zipEnt.getName();   
102.                     strtemp = strPath + File.separator + gbkPath;   
103.                    
104. //建目录 
105.                     String strsubdir = gbkPath;   
106. for(int i = 0; i < strsubdir.length(); i++) {   
107. if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {   
108. 0, i);   
109. new
110. if(!subdir.exists())   
111.                             subdir.mkdir();   
112.                         }   
113.                     }   
114. new
115. new
116. int
117. while((c = bis.read()) != -1) {   
118. byte) c);   
119.                     }   
120.                     bos.close();   
121.                     fos.close();   
122.                 }   
123.             }   
124. catch(Exception e) {   
125.             e.printStackTrace();   
126. throw
127.         }   
128.     }   
129. /** 
130.      * 压缩zip格式的压缩文件 
131.      * @param inputFilename 压缩的文件或文件夹及详细路径 
132.      * @param zipFilename 输出文件名称及详细路径 
133.      * @throws IOException 
134.      */
135. public synchronized void zip(String inputFilename, String zipFilename) throws
136. new
137.     }   
138.        
139. /** 
140.      * 压缩zip格式的压缩文件 
141.      * @param inputFile 需压缩文件 
142.      * @param zipFilename 输出文件及详细路径 
143.      * @throws IOException 
144.      */
145. public synchronized void zip(File inputFile, String zipFilename) throws
146. new ZipOutputStream(new
147. try
148. "");   
149. catch
150. throw
151. finally
152.             out.close();   
153.         }   
154.     }   
155.        
156. /** 
157.      * 压缩zip格式的压缩文件 
158.      * @param inputFile 需压缩文件 
159.      * @param out 输出压缩文件 
160.      * @param base 结束标识 
161.      * @throws IOException 
162.      */
163. @SuppressWarnings("unused")   
164. private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws
165. if
166.             File[] inputFiles = inputFile.listFiles();   
167. new ZipEntry(base + "/"));   
168. 0 ? "" : base + "/";   
169. for (int i = 0; i < inputFiles.length; i++) {   
170.                 zip(inputFiles[i], out, base + inputFiles[i].getName());   
171.             }   
172. else
173. if (base.length() > 0) {   
174. new
175. else
176. new
177.             }   
178. new
179. try
180. int
181. byte[] by = new byte[BUFFEREDSIZE];   
182. while ((c = in.read(by)) != -1) {   
183. 0, c);   
184.                 }   
185. catch
186. throw
187. finally
188.                 in.close();   
189.             }   
190.         }   
191.     }   
192. /** 
193.      * 解压rar格式的压缩文件到指定目录下 
194.      * @param rarFileName 压缩文件 
195.      * @param extPlace 解压目录 
196.      * @throws Exception 
197.      */
198. public synchronized void unrar(String rarFileName, String extPlace) throws
199. try
200. new
201. // 构建测解压缩类 
202. new
203. false); //不输出日志 
204. // 设置rar文件 
205.             archive.setFile(rarFileName);   
206.             archive.setExtractPath(extPlace);   
207.             archive.extractAllFile();   
208. catch
209. // TODO: handle exception 
210.         }   
211.     }}