4 Replies Latest reply: Apr 23, 2009 8:50 AM by 800344 RSS

    create a zip files in a special way

    800344
      Dear all,

      Following my yesterday's posting, I would clarify my questions:

      I successfully created a zip file which contains a folder structure and two files from strings. See attached codes.
      However, I don't like the way that I physically created subfolder and files before add them to the zip file.
      Is there any possibility to avoid that part of creating multiple folders?

      Regards.

      Pengyou
      package com.zip.test;
      
      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.BufferedWriter;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;
      
      public class ZipTest {
           static final int BUFFER = 2048;
      
           public static void main(String argv[]) {
                try {
                     BufferedInputStream origin = null;
                     FileOutputStream dest = new FileOutputStream("mytest.zip");
                     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                               dest));
                     // out.setMethod(ZipOutputStream.DEFLATED);
                     byte data[] = new byte[BUFFER];
      
                     try {
                          String strManyDirectories = "root/dir1/dir2";
                          // Create multiple directories
                          boolean success = (new File(strManyDirectories)).mkdirs();
                          if (success) {
                               System.out.println("Directories: " + strManyDirectories
                                         + " created");
                          }
                          File outputFile = new File("root/dir1/dir2/test.html");
                          BufferedWriter bufferedWriter = new BufferedWriter(
                                    new FileWriter(outputFile));
                          bufferedWriter.write("Hello, Test");
                          bufferedWriter.close();
                          File outputFile1 = new File("root/dir1/dir2/test1.html");
                          BufferedWriter bufferedWriter1 = new BufferedWriter(
                                    new FileWriter(outputFile1));
                          bufferedWriter1.write("Hello, Test1");
                          bufferedWriter1.close();
      
                     } catch (IOException e) {
                     }
      
                     String files[] = { "root/dir1/dir2/test.html",
                               "root/dir1/dir2/test1.html" };
      
                     for (int i = 0; i < files.length; i++) {
                          System.out.println("Adding: " + files);
                          FileInputStream fi = new FileInputStream(files[i]);
                          origin = new BufferedInputStream(fi, BUFFER);
                          ZipEntry entry = new ZipEntry(files[i]);
                          out.putNextEntry(entry);
                          int count;
                          while ((count = origin.read(data, 0, BUFFER)) != -1) {
                               out.write(data, 0, count);
                          }
                          origin.close();
                     }
                     out.close();
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }

      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
        • 1. Re: create a zip files in a special way
          thomas.behr
          Why do you create files from your String data? I told you in your previous thread to write the String data to the ZipOutputStream directly using a Writer (see [http://forums.sun.com/thread.jspa?messageID=10683952#10683952]).
          • 2. Re: create a zip files in a special way
            800344
            thomas.behr wrote:
            Why do you create files from your String data? I told you in your previous thread to write the String data to the ZipOutputStream directly using a Writer (see [http://forums.sun.com/thread.jspa?messageID=10683952#10683952]).
            Indeed, you gave some good ideas.
            But I did not understand how to convert a String to ZipOutputStream with a user choosen file name and a user choosen folder structure.
            I am blocked at that point. Sorry for repeating the same question.
            • 3. Re: create a zip files in a special way
              thomas.behr
              pengyou wrote:
              But I did not understand how to convert a String to ZipOutputStream with a user choosen file name and a user choosen folder structure.
              I am blocked at that point. Sorry for repeating the same question.
              That doesn't make sense to me. Even more so as your sample code suggests that you are familiar with Writer and ZipEntry concepts. Can you explain in detail where you are stuck or what part of my previous suggestion was unclear to you?

              Scratch that, I think I finally understand your problem. I think the following sample code will get you on track:
              import java.util.zip.ZipOutputStream;
              import java.util.zip.ZipEntry;
              import java.io.FileOutputStream;
              import java.io.BufferedWriter;
              import java.io.OutputStreamWriter;
              
              public class ZipTest {
                  public static void main( String[] args ) throws Exception {
                      final String[] content = {"bla", "blubber"};
                      final String[] names = {"root/content1/c1.txt", "root/content2/c2.txt"};
              
                      final String path = System.getProperty("user.home") + "/test.zip";
              
                      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path));
                      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(zos, "UTF-8"));
              
                      for (int i = 0; i < content.length; ++i) {
                          final ZipEntry entry = new ZipEntry(names);
              zos.putNextEntry(entry);

              bw.write(content[i]);
              bw.flush();

              zos.closeEntry();
              }

              // this will close zos, too
              bw.close();
              }
              }


              Edited by: thomas.behr on Apr 22, 2009 4:10 PM
              • 4. Re: create a zip files in a special way
                800344
                Scratch that, I think I finally understand your problem. I think the following sample code will get you on track:
                Thomas,

                Thanks! I got your idea.
                I was blocked due to the fact I don't know
                 bw.write(content); 
                   
                It is really what I want.
                Pengyou