How to select only .txt file to be zipped using java?

How to select only .txt file to be zipped using java?


i'm trying to zipp .txt files from a folder but i want to know how to select only .txt file. I try my code but it's not working, could any one help me please.

[CODE] here is the code: import java.util.*; import*; import*;

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\\");//here i want to put zipped file in a different directory

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);

    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");
    byte[] buf = new byte[2048];
    int len;
    while ((len = > 0) {
        out.write(buf, 0, len);
catch(IOException e) {
finally {
    try {
        if(in != null) {
        if(out != null) {
    catch(IOException ignored)
            { }

} [/CODE]

Thank you

View Answers

March 1, 2011 at 1:43 PM

Java create zip file

class OnlyExt implements FilenameFilter{
  String ext;
  public OnlyExt(String ext){
  this.ext="." + ext;
  public boolean accept(File dir,String name){
  return name.endsWith(ext);
public class CreateZip{
public static int buffer = 10240;
protected void create(File exefile, File[] listFiles){
byte b[] = new byte[buffer];
FileOutputStream fout = new FileOutputStream(exefile);
ZipOutputStream out = new ZipOutputStream(fout);
for(int i = 0; i < listFiles.length; i++){
if(listFiles[i] == null || !listFiles[i].exists()|| listFiles[i].isDirectory())
System.out.println("Adding " + listFiles[i].getName());
ZipEntry addFiles = new ZipEntry(listFiles[i].getName());

FileInputStream fin = new FileInputStream(listFiles[i]);
int len =, 0, b.length);
if(len <= 0)
out.write(b, 0, len);
System.out.println("Zip File is created successfully.");
catch (Exception ex){}
public static void main(String[]args){
CreateZip exe=new CreateZip();
FilenameFilter ff = new OnlyExt("txt");
File folder = new File("c:/");
File[] files = folder.listFiles(ff);
File file=new File("C:/");
exe.create(file, files);

March 1, 2011 at 2:13 PM

Thank you very much


Related Tutorials/Questions & Answers:
How to select only .txt file to be zipped using java?
How to import txt file using SQL
how to save html form data into .csv file using only jsp.
Javah -  Header File Generator
How to passed the ID's in MAP how to stored whaterver select the name that only ID should store in db using SWING/AWT?
how to change file from .txt to .mat(matrix)
Convesion of txt file to doc file.??????
Thread for reading txt file
How to make a file read-only in java
How to check if a file is read-only in java
how to operate on select box using ajax in struts2?
how to operate on select box using ajax in struts2?
How to give value for select in HTML dynamically using javascript
How to make session control using only JSP programming language
Backup Into txt File
How to play only video file from any link
How to zip a file using javascrip?
How to write to file using FileWriter
How to write to file using FileOutputStream
ModuleNotFoundError: No module named 'file_open_gips_txt'
How to upload file using JSP?
Uploading and download pdf or .txt file.
Backup selected records into txt file
How to get the full path location using <input type=file>
Backup Into txt File
how to create an excel file using java
How to open a file in java using JFileChooser
How to transfer a file in java using SSH & Telnet
how to read and write an xml file using java