如何在java中执行zip压缩命令

sparadise1003 2009-10-15 10:23:42
zip.exe存放在c:\TC\bin\zip.exe ,我现在想在java中通过调用zip命令将c:\temp\中的1.txt 2.txt 打包生成名为test.zip

在dos里我会这个操作我会:zip -1 test.zip 1.txt 2.txt

可是在java 中我就不会了,我的java代码如下:

……
Runtime rt = Runtime.getRuntime(); //获取系统当前运行时对象
Process p = null; //创建一个进程

try{

//p=rt.exec("\"D:/AnyQ/AnyQ.exe\"");
p = rt.exec( 这里我该怎么写呢 );

}catch(Exception e){

System.out.println("Error exec AnyQ");

}
……
...全文
622 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lz12366007 2009-10-15
  • 打赏
  • 举报
回复
我觉得用流 更好些!!!个人观点
lz12366007 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 withoutme_hw 的回复:]
Java code Runtime runtime= Runtime.getRuntime();try
{
runtime.exec("WinRAR x D:\\data.zip *.txt D:\\");
}catch (IOException e)
{
e.printStackTrace();
}
以上代码测试成功,D:\\data.zip 为要解压的文件的路径及文件名, *.txt是指把zip中所有的txt加压出来,D:\\是指解压到D盘根目录下。

前提是在Windows环境变量中,将WinRAR的路径加入到Path变量里
[/Quote]
如果不想设置的话 那就弄winRaR的绝对路径
sparadise1003 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 withoutme_hw 的回复:]
Java code Runtime runtime= Runtime.getRuntime();try
{
runtime.exec("WinRAR x D:\\data.zip *.txt D:\\");
}catch (IOException e)
{
e.printStackTrace();
}
以上代码测试成功,D:\\data.zip 为要解压的文件的路径及文件名, *.txt是指把zip中所有的txt加压出来,D:\\是指解压到D盘根目录下。

前提是在Windows环境变量中,将WinRAR的路径加入到Path变量里
[/Quote]
WinRAR 这个命令貌似不是所有电脑都可以用吧,我电脑里只有zip.exe
sparadise1003 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 swandragon 的回复:]
c:\TC\bin\zip.exe放到path环境变量中

Runtime.getRuntime().exec("cmd /c zip -1 test.zip 1.txt 2.txt");
[/Quote]

我试了,不好用啊
老张-AI 2009-10-15
  • 打赏
  • 举报
回复
写个批处理
java调用
liwenso 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 swandragon 的回复:]
c:\TC\bin\zip.exe放到path环境变量中

Runtime.getRuntime().exec("cmd /c zip -1 test.zip 1.txt 2.txt");
[/Quote]

顶LS
王铎开 2009-10-15
  • 打赏
  • 举报
回复
路过
swandragon 2009-10-15
  • 打赏
  • 举报
回复
c:\TC\bin\zip.exe放到path环境变量中

Runtime.getRuntime().exec("cmd /c zip -1 test.zip 1.txt 2.txt");
withoutme_hw 2009-10-15
  • 打赏
  • 举报
回复
		Runtime runtime = Runtime.getRuntime();
try
{
runtime.exec("WinRAR x D:\\data.zip *.txt D:\\");
}
catch (IOException e)
{
e.printStackTrace();
}

以上代码测试成功,D:\\data.zip 为要解压的文件的路径及文件名, *.txt是指把zip中所有的txt加压出来,D:\\是指解压到D盘根目录下。

前提是在Windows环境变量中,将WinRAR的路径加入到Path变量里
wind198299 2009-10-15
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.zip.*;

public class SimpleJavaZip {

public static void main(String[] args) {
try {
String Filename = args[0] + ".zip";
FileOutputStream f = new FileOutputStream(Filename);
ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));
for (int i = 1; i < args.length; ++i) {
System.out.println("Zipping file " + args[i]);
DataInputStream in = new DataInputStream(new FileInputStream(
args[i]));
out.putNextEntry(new ZipEntry(args[i]));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}

}
/*
* 这个程序从控制台的第一个参数接收 将要生成的压缩文件名字, 如test.zip等, 而从第二个参数开始, 用于接收要压缩的文件,
* 在程序中,用了一个for循环来接收第二个参数开始所表示的文件. 执行如下 命令,可以得到一个名为 "test.zip" 的压缩文件. java
* SimpleJavaZip test a.htm b.txt c.doc e.bmp f.gif
* 这个程序只能对文件进行压缩,但是我们在实际应用中, 更加需要的是对目录进行压缩. 因此,需要对上面的 程序进行修改, 让它可以处理目录.
*
*/
}


swandragon 2009-10-15
  • 打赏
  • 举报
回复
写成bat,java调用bat

cd c:\TC\bin\zip.exe
zip -1 test.zip 1.txt 2.txt

sparadise1003 2009-10-15
  • 打赏
  • 举报
回复
终于有答案了,直接这么写就行了:
Runtime.getRuntime().exec("zip -1 路径:\\test.zip 路径:\\1.txt 路径:\\2.txt");

关于写成批处理然后再调用的方法:
java现在不能直接调用批处理程序,需要将批处理转成exe然后再调用,相对来说稍麻烦一下。
顺便说一下,Quick Batch File (De)Compiler可以将批处理转成.exe

多谢各位朋友的帮忙,贴子我已经加分了,由于个人分不多,就加到50吧。
sparadise1003 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 swandragon 的回复:]
c:\TC\bin\zip.exe放到path环境变量中

Runtime.getRuntime().exec("cmd /c zip -1 test.zip 1.txt 2.txt");
[/Quote]

zip.exe我试了一下在哪个目录里都好用,因此就不用设置环境变量了。上面的代码我觉也应该好用啊,可是执行完毕后没有任何反应也不报错,谁知道上面的代码哪里有问题?

62,621

社区成员

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

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