这里我想压缩文件夹中的.txt文件使用java。如何在使用java的文件夹中仅压缩.txt文件?
我的代码在这里找到与谷歌和完美的作品,但只为一个指定的.txt文件。
谢谢。
import java.util.*;
import java.util.zip.*;
import java.io.*;
public class ZipFile
{
public static void main(String[] args) {
ZipOutputStream out = null;
InputStream in = null;
try {
File inputFile1 = new File("c:\\Target\\target.txt");// here i want to say only the directroy where .txt files are stored
File outputFile = new File("c:\\Target\\Archive_target.zip");//here i want to put zipped file in a different directory
OutputStream rawOut = new BufferedOutputStream(new FileOutputStream(outputFile));
out = new ZipOutputStream(rawOut);
InputStream rawIn = new FileInputStream(inputFile1);
in = new BufferedInputStream(rawIn);
ZipEntry entry = new ZipEntry("c:\\Target\\target.txt");
out.putNextEntry(entry);
byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
}
catch(IOException ignored)
{ }
}
}
}
好吧,当我尝试你的小费,我有这样的消息: – user618111 2011-02-27 20:43:38
java.io.FileNotFoundException:C:\目标(访问被拒绝) 是java。 io.FileInputStream.o笔(Native Method) 位于java.io.FileInputStream。(FileInputStream.java:106) at ZipFile.main(ZipFile.java:37) –
user618111
2011-02-27 20:44:21
@ user618111:请在编辑问题时显示您已完成的操作和错误消息。这样可以更容易地帮助你。 (它看起来像目录不可读,或者您正在尝试将目录作为文件读取)。 – 2011-02-27 21:56:40