Discussions

EJB design: Please help-How do I zip up folders

  1. Please help-How do I zip up folders (2 messages)

    This is the codes that I use to zip up files but now I need to zip up folders so anyone have an example to show?
    Creating a ZIP File
        // These are the files to include in the ZIP file
        String[] filenames = new String[]{"filename1", "filename2"};
        
        // Create a buffer for reading the files
        byte[] buf = new byte[1024];
        
        try {
            // Create the ZIP file
            String outFilename = "outfile.zip";
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
        
            // Compress the files
            for (int i=0; i<filenames.length; i++) {
                FileInputStream in = new FileInputStream(filenames[i]);
        
                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(filenames[i]));
        
                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
        
                // Complete the entry
                out.closeEntry();
                in.close();
            }
        
            // Complete the ZIP file
            out.close();
        } catch (IOException e) {
        }
  2. Links[ Go to top ]

    Hi Please see the links below .

    http://forum.java.sun.com/thread.jspa?threadID=448233&tstart=135

    http://forum.java.sun.com/thread.jspa?threadID=325256

    Hope these links will help to solve your problem.

    Mohit
  3. This are the codes that I extracted it out from the links given but it does not zip up folders and no .zip folder is created.

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
     
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    import java.util.zip.ZipException;
     
     
     
     
    /**
     * Demo for using the Zip facilities to compress data
     */
     
    public class ZipDemo
    {
        /** Default buffer size */
        private static final int DEFAULT_BUFFER_SIZE = 4096;
     
     
     
     
        /**
         * Compress a string
         * @param uncompressed string
         * @return byte array containing compressed data
         * @throws IOException if the deflation fails
         */
     
        public static final byte [] compress(final String uncompressed) throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            GZIPOutputStream zos = new GZIPOutputStream(baos);
     
            byte [] uncompressedBytes = uncompressed.getBytes();
     
            zos.write(uncompressedBytes, 0, uncompressedBytes.length);
            zos.close();
     
            return baos.toByteArray();
        }
     
     
     
     
        /**
         * Uncompress a previously compressed string;
         * this method is the inverse of the compress method.
         * @param byte array containing compressed data
         * @return uncompressed string
         * @throws IOException if the inflation fails
         */
     
        public static final String uncompress(final byte [] compressed) throws IOException
        {
            String uncompressed = "";
     
            try
            {
                ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
                GZIPInputStream zis = new GZIPInputStream(bais);
     
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int numBytesRead = 0;
                byte [] tempBytes = new byte[DEFAULT_BUFFER_SIZE];
                while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1)
                {
                    baos.write(tempBytes, 0, numBytesRead);
                }
     
                uncompressed = new String(baos.toByteArray());
            }
            catch (ZipException e)
            {
                e.printStackTrace(System.err);
            }
     
            return uncompressed;
        }
     
     
     
     
        /**
         * Uncompress a previously compressed string;
         * this method is the inverse of the compress method.
         * Implemented in terms of the byte array version.
         * @param string containing compressed data
         * @return uncompressed string
         * @throws IOException if the inflation fails
         */
     
        public static final String uncompress(final String compressed) throws IOException
        {
            return ZipDemo.uncompress(compressed.getBytes());
        }
     
     
     
        /**
         * Main driver class for ZipDemo
         * @param command line arguments - either string or file name to compress
         */
     
        public static void main(String [] args)
        {
            try
            {
                for (int i = 0; i < args.length; ++i)
                {
                    String uncompressed = "";
                    File f = new File(args[i]);
     
                    if (f.exists())
                    {
                        BufferedReader br = new BufferedReader(new FileReader(f));
     
                        String line = "";
                        StringBuffer buffer = new StringBuffer();
     
                        while ((line = br.readLine()) != null)
                            buffer.append(line);
     
                        br.close();
     
                        uncompressed = buffer.toString();
                    }
                    else
                    {
                        uncompressed = args[i];
                    }
     
                    System.out.println("length before compression: " + uncompressed.length());
     
                    byte [] compressed = ZipDemo.compress(uncompressed);
     
                    System.out.println("length after compression : " + compressed.length);
     
                    String compressedAsString = new String(compressed);
                    System.out.println("length of compressed str : " + compressedAsString.length());
     
                    byte [] bytesFromCompressedAsString = compressedAsString.getBytes();
     
                    boolean isTheSameAs = bytesFromCompressedAsString.equals(compressed);
                    System.out.println("compressed bytes are " + (isTheSameAs ? "" : "not ") + "the same as from String");
                    System.out.println("length of bytesFrom...: " + bytesFromCompressedAsString.length);
     
                    String restored = ZipDemo.uncompress(compressed);
     
                    System.out.println("length after decompress : " + restored.length());
     
                    isTheSameAs = restored.equals(uncompressed);
                    System.out.println("original is " + (isTheSameAs ? "" : "not ") + "the same as the restored");
     
                    String restoredFromString = ZipDemo.uncompress(compressedAsString);
                    isTheSameAs = restoredFromString.equals(uncompressed);
                    System.out.println("original is " + (isTheSameAs ? "" : "not ") + "the same as the restored from string");
                }
            }
            catch (Exception e)
            {
                e.printStackTrace(System.err);
            }
        }
    }



    Please help,thanks!