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

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

Hello,

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

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

} [/CODE]

Thank you

View Answers

March 1, 2011 at 1:43 PM

Java create zip file

import java.io.*;
import java.util.zip.*;
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){
try{
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());
addFiles.setTime(listFiles[i].lastModified());
out.putNextEntry(addFiles);

FileInputStream fin = new FileInputStream(listFiles[i]);
while(true){
int len = fin.read(b, 0, b.length);
if(len <= 0)
break;
out.write(b, 0, len);
}
fin.close();
}
out.close();
fout.close();
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:/Examples.zip");
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
Advertisements
how to save html form data into .csv file using only jsp.
Javah -  Header File Generator
how to change file from .txt to .mat(matrix)
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?
difference between java5 and java6 - Java Beginners
how to start intergrated webcam of a laptop without calling its exe file using java only - Java Beginners
Convesion of txt file to doc file.??????
Thread for reading txt file
Java2
How to make a file read-only
Javah
How to make a file read-only in java
Backup Into txt File
about java1
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?
ModuleNotFoundError: No module named 'file_open_gips_txt'
Uploading and download pdf or .txt file.
How to give value for select in HTML dynamically using javascript
How to make session control using only JSP programming language
How to play only video file from any link
Backup selected records into txt file
How to add another option to the select tag using struts2 tag - Struts
How to read data from txt file and save to database MySql - Java Beginners
Backup Into txt File
Shifting txt file to database - Java Beginners
javaa swings - IDE Questions
About Java2
How to zip a file using javascrip?
How to write to file using FileOutputStream
How to write to file using FileWriter
How to upload file using JSP?
Backup selected records into txt file
Remove JTable row that read txt file records
upload only pdf file - Struts
convert .txt file in .csv format - Java Beginners
select query using hibernate
How to get the full path location using <input type=file>
upload the pdf file only - Struts
Select Records Using Prepared Statement
Maven dependency for com.gooddata - gooddata-http-client version 1.0.0+java7 is released. Learn to use gooddata-http-client version 1.0.0+java7 in Maven based Java projects
i have one txt field and one button.when i entere any test in testfield then only button should be enabled.
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
Solve using only Javascript loops...
how to read and write an xml file using java

Ads