title: How to zip only .txt file in a folder using java?
body: <p>here i'm trying to zip only .txt file in a folder using java.</p>
  
  <p>My code here was found with google and works perfectly but only for a specified .txt file.</p>
  
  <p>Thank you.</p>
  
  <pre><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
  
          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)) &gt; 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></pre>