Infolinks In Text Ads

Zipping Multiple Files In java

How to zip multiple files in java.

  • First i have created files and save on server side.
  • Then i have reading the file names and zip the created files.
  • When zip file are created ,then delete those created file.

First i have created two files and save on server side.
      List<String> fileText = new ArrayList<String>(1);
            List<String> ListOfFiles = new ArrayList<String>(1);
            fileText.add("text file 1");
            fileText.add("text file 2");
            ListOfFiles.add("1");
            ListOfFiles.add("2");
The below code is used to read the created file names and then zipping the created files on server side
List<String> fileNames = new ArrayList<String>(1);
            File fileTEXT = null;
            BufferedWriter output = null;
            try {

                  for (int i = 0; i < fileText.size(); i++) {
                        String fileName = "E:/C-CDA/MU2_CDA_WORKSPACE/zippingFiles/example"
                                    + ListOfFiles.get(i) + ".txt";
                        System.out.println("file name are :" + fileName);
                        fileTEXT = new File(fileName);
                        output = new BufferedWriter(new FileWriter(fileTEXT));
                        output.write(fileText.get(i));
                        fileNames.add(fileName);

                        output.close();
                  }

       

      // out put file
                  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                              "E:/C-CDA/MU2_CDA_WORKSPACE/zippingFiles/tmp.zip"));
                  // input file

                  // FileInputStream in = new
                  // FileInputStream("E:/C-CDA/MU2_CDA_WORKSPACE/zippingFiles/example.txt");
                  FileInputStream fis = null;
                  // name the file inside the zip file
                  for (int i = 0; i < fileNames.size(); i++) {
                        fis = new FileInputStream(fileNames.get(i).toString());
                        BufferedInputStream bis = new BufferedInputStream(fis);
                        // Add ZIP entry to output stream.
                        File file = new File(fileNames.get(i).toString());
                        String entryname = file.getName();
                        out.putNextEntry(new ZipEntry(entryname));

                        // buffer size
                        byte[] b = new byte[1024];
                        int count;

                        while ((count = fis.read(b)) > 0) {
                              System.out.println();
                              out.write(b, 0, count);
                        }
                  }
                  out.close();
                  fis.close();

            } catch (IOException e) {
                  e.printStackTrace();
            }

            finally {
                  System.out.println("delete created files after zipping");
                  for (int i = 0; i < fileNames.size(); i++) {
                        File ac = new File(fileNames.get(i));
                        ac.delete();
                  }
            }
      }
}

Newer Post

Leave a Reply

Powered by Blogger.