如何使用java仅压缩文件夹中的.txt文件?

发布于 2024-10-19 08:30:48 字数 1394 浏览 3 评论 0原文

在这里,我尝试使用 java 仅压缩文件夹中的 .txt 文件。

我的代码是通过 google 找到的,并且可以完美运行,但仅适用于指定的 .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)
                { }
    }
    }
}

here i'm trying to zip only .txt file in a folder using java.

My code here was found with google and works perfectly but only for a specified .txt file.

Thank you.

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)
                { }
    }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

讽刺将军 2024-10-26 08:30:48

您需要使用 File.list(...) 获取文件夹中所有文本文件的列表。然后创建一个循环将每个文件写入 zip 文件。

You need to use File.list(...) to get a list of all the text files in the folder. Then you create a loop to write each file to the zip file.

心作怪 2024-10-26 08:30:48

我只是在之后添加这些行
"文件输出文件 = new File("c:\Target\Archive_target.zip");
从我以前的代码。

添加代码:

File Dir = new File("c:/Target");
            FilenameFilter filter = new FilenameFilter() {
      public boolean accept(File dir, String name) {
        return !name.startsWith(".txt");
      }
    };
    String[] children = Dir.list(filter);

I just add these lines just after
"File outputFile = new File("c:\Target\Archive_target.zip");
from my previous code.

code added:

File Dir = new File("c:/Target");
            FilenameFilter filter = new FilenameFilter() {
      public boolean accept(File dir, String name) {
        return !name.startsWith(".txt");
      }
    };
    String[] children = Dir.list(filter);
终难愈 2024-10-26 08:30:48

您可以使用 File 类的以下方法获取目录中所有文本文件的列表:
String[]列表(FilenameFilter过滤器)
创建一个指向您的 DIRECTORY 的 File 对象(我知道这听起来不合逻辑,但事实就是这样 - 您可以使用 isDirectory() 测试它是否是一个目录),然后使用 FilenameFilter 来表示,例如,接受此文件如果其名称包含“.txt”

You can get a list of all text files in your directory by using the following method of the File class:
String[] list(FilenameFilter filter)
Create a File object that points to your DIRECTORY (I know it sounds illogical, but that's the way it is- you can test if it is a directory using isDirectory()) and then use the FilenameFilter to say, for example, accept this file if its name contain ".txt"

讽刺将军 2024-10-26 08:30:48

创建一个仅接受 *.txt 文件的 FilenameFilter ,然后只需使用

list = File.list(yourNameFilter);

然后将列表中的所有文件添加到 zip 文件中

Create a FilenameFilter that accepts only *.txt file , and then just use

list = File.list(yourNameFilter);

and then just add all the files in the list to the zip file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文