java 压缩/解压 .zip/.rar/.tar 文件,大家一起讨论,学习

every888 2009-02-27 11:35:23
公司要开发一套服务器远程维护系统,
其中有一个模块就是压缩下载,上传解压
考虑到跨平台及方便用户,至少要支持上面的几种格式
大家一起讨论下用java代码怎么实现那几种格式的压缩与解压缩

谢谢
...全文
3801 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhenshixian88 2012-05-11
  • 打赏
  • 举报
回复
好贴,最近刚好用的上
xstartv 2011-06-07
  • 打赏
  • 举报
回复
顶啊 好帖子 支持
every888 2009-04-20
  • 打赏
  • 举报
回复
终于解决了
又需要的朋友可以到我上传的资源里下载
也可以到编程交流群10939695共享空间下载
谢谢
SheLiZi 2009-04-14
  • 打赏
  • 举报
回复
学习
tangxiucai2 2009-04-14
  • 打赏
  • 举报
回复
关注。。
firezxm 2009-04-13
  • 打赏
  • 举报
回复
hao de .积分
woshixiaoyueer 2009-04-11
  • 打赏
  • 举报
回复

招聘Flash游戏开发(java)
要求:
1.两年以上java开发经验。
2.精通面向对象的分析和设计技术,包括设计模式、UML建模等。
3.精通Java SE和Java EE技术。
4.熟悉Linux操作系统,可以熟练使用常用的Linux命令完成日常工作。
5.熟悉oracle、mysql、sqlserver等数据库,对oracle数据库深入的理解。
6.对各种开源的框架如Spring、Hibernate、struts、webwork2等有深入的了解,读过源代码者尤佳。
7.熟悉freemaker模板引擎。
8.有大规模高并发访问的网站架构设计和开发经验优先。
9.专注于技术,精益求精。
10.思路清晰,沟通能力强
有意者请发送简历至:zhengmingrunhua@gmail.com
(简历主题写招聘职位)
公司:正明润华 地址:北京朝阳望京
every888 2009-04-07
  • 打赏
  • 举报
回复
怎么解压tar的?怎么解压tar的?怎么解压tar的?怎么解压tar的?
SZ深呼吸 2009-04-07
  • 打赏
  • 举报
回复
网上搜一下吧,很多的
给个参考http://www.javaeye.com/topic/350001
every888 2009-04-06
  • 打赏
  • 举报
回复
怎么解压tar的?
myairland 2009-04-04
  • 打赏
  • 举报
回复
顶17楼,不过虽然是有版权的,网上仍能下到非法的JAR报来解压缩的
skycc999 2009-04-03
  • 打赏
  • 举报
回复
学习了,ant.jar。
bootupnow 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 bao110908 的回复:]
而 ZIP 压缩算法是免费开放的,任何人可以免费使用。但是 RAR 就不一样了,
这个压缩算法已经受到专利权的保护,如果要使用 RAR 算法必须向其专利所有
人支付费用。

中国人用惯了盗版的 WinRAR,以为 RAR 才是压缩格式的王道,自以为是的以为
全天下的压缩软件都应该支持 RAR 格式。

曾看到有些人做了一些东西并且发布成开源的,但是他们是采用 RAR 格式打包的,
要知道这是一种侵犯知识产权的行为。之所以所有的开源框…
[/Quote]
果大的话很有深度,学习mark!
every888 2009-03-30
  • 打赏
  • 举报
回复
怎么解压tar的?
chensjmail 2009-03-18
  • 打赏
  • 举报
回复
mark
every888 2009-03-09
  • 打赏
  • 举报
回复
zip的可以压缩,解压了,rar的也可以解压缩了,tar的还没有实现,先贴出来大家一起学习

导入的关键包我已经上传到QQ群10939695共享空间内
有什么疑问可以在群里问也可以在csdn上给我留言





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;

import de.innosystec.unrar.Archive;

/**
* @author Every E-mail/MSN:mwgjkf@hotmail.com
* QQ:30130942
* @version 创建时间:Feb 26, 2009 6:01:11 PM
* 类说明:压缩、解压文件公用类
*
*/
public class Decompression {
private static final int BUFFEREDSIZE = 1024;

/**
* 解压zip格式的压缩文件到指定位置
* @param zipFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
*/
@SuppressWarnings("unchecked")
public synchronized void unzip(String zipFileName, String extPlace) throws Exception {
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName);
if((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while(e.hasMoreElements()){
org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath=zipEnt.getName();
if(zipEnt.isDirectory()){
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else {
//读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath=zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;

//建目录
String strsubdir = gbkPath;
for(int i = 0; i < strsubdir.length(); i++) {
if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if(!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while((c = bis.read()) != -1) {
bos.write((byte) c);
}
bos.close();
fos.close();
}
}
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}

/**
* 解压zip格式的压缩文件到指定位置
* @param zipFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
*/
@SuppressWarnings("unchecked")
public synchronized void unzip(String zipFileName, String extPlace,boolean whether) throws Exception {
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName);
if((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while(e.hasMoreElements()){
org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath=zipEnt.getName();
if(zipEnt.isDirectory()){
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else {
//读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath=zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;

//建目录
String strsubdir = gbkPath;
for(int i = 0; i < strsubdir.length(); i++) {
if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if(!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while((c = bis.read()) != -1) {
bos.write((byte) c);
}
bos.close();
fos.close();
}
}
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 压缩zip格式的压缩文件
* @param inputFilename 压缩的文件或文件夹及详细路径
* @param zipFilename 输出文件名称及详细路径
* @throws IOException
*/
public synchronized void zip(String inputFilename, String zipFilename) throws IOException {
zip(new File(inputFilename), zipFilename);
}

/**
* 压缩zip格式的压缩文件
* @param inputFile 需压缩文件
* @param zipFilename 输出文件及详细路径
* @throws IOException
*/
public synchronized void zip(File inputFile, String zipFilename) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
try {
zip(inputFile, out, "");
} catch (IOException e) {
throw e;
} finally {
out.close();
}
}

/**
* 压缩zip格式的压缩文件
* @param inputFile 需压缩文件
* @param out 输出压缩文件
* @param base 结束标识
* @throws IOException
*/
@SuppressWarnings("unused")
private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
/**
* 解压rar格式的压缩文件到指定目录下
* @param rarFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
*/
public synchronized void unrar(String rarFileName, String extPlace) throws Exception{
try {
(new File(extPlace)).mkdirs();
// 构建测解压缩类
Archive archive = new Archive();
archive.setEnabledLog(false); //不输出日志
// 设置rar文件
archive.setFile(rarFileName);
archive.setExtractPath(extPlace);
archive.extractAllFile();
} catch (Exception e) {
// TODO: handle exception
}
}

}

ZHANGBINFLY 2009-03-08
  • 打赏
  • 举报
回复
up,zipoutputstream
  • 打赏
  • 举报
回复
RAR 压缩算法属于商业机密,WinRAR 也是共享版软件,可能都用惯了非法授权的软件就认为是理所当然了!
  • 打赏
  • 举报
回复
而 ZIP 压缩算法是免费开放的,任何人可以免费使用。但是 RAR 就不一样了,
这个压缩算法已经受到专利权的保护,如果要使用 RAR 算法必须向其专利所有
人支付费用。

中国人用惯了盗版的 WinRAR,以为 RAR 才是压缩格式的王道,自以为是的以为
全天下的压缩软件都应该支持 RAR 格式。

曾看到有些人做了一些东西并且发布成开源的,但是他们是采用 RAR 格式打包的,
要知道这是一种侵犯知识产权的行为。之所以所有的开源框架都是采用 zip 打包
的也就是这个原因了。
moonshowder 2009-03-06
  • 打赏
  • 举报
回复

unrarCmd=C:\\WinRAR\\UnRar x
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(unrarCmd + 文件名);
加载更多回复(14)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧