Home Answers Viewqa Java-Beginners How to select only .txt file to be zipped using java?

 
 


HHH
How to select only .txt file to be zipped using java?
2 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners

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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.