Problem with these codes,anyone knows thw solution to it?

Discussions

EJB design: Problem with these codes,anyone knows thw solution to it?

  1. hi all,this is folderziper codes.It looks fine and when run it,there is a zipped up folder being created but when i want to extract all from this zipped folder there's a pop up saying "Windows has blocked access to these files to help protect your computer".It cannot be extracted.I think maybe the codes are somehow not correct.The following is the codes,someone show how to solve it, can post here.Thanks!

    import java.io.File;
    import java.util.zip.ZipOutputStream;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.util.zip.ZipEntry;
    /**
     * FolderZiper provide a static method to zip a folder.
     */
    public class FolderZiper {

      /**
       * Zip the srcFolder into the destFileZipFile. All the folder subtree of the src folder is added to the destZipFile
       * archive.
       *
       * TODO handle the usecase of srcFolder being en file.
       *
       * @param srcFolder String, the path of the srcFolder
       * @param destZipFile String, the path of the destination zipFile. This file will be created or erased.
       */
        public static void zipFolder(String srcFolder, String destZipFile) {
          ZipOutputStream zip = null;
          FileOutputStream fileWriter = null;
          try {
            fileWriter = new FileOutputStream(destZipFile);
            zip = new ZipOutputStream(fileWriter);
          }
          catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
          }

          addFolderToZip("", srcFolder, zip);
          try {
            zip.flush();
            zip.close();
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }
        }

        /**
         * Write the content of srcFile in a new ZipEntry, named path+srcFile, of the zip stream. The result
         * is that the srcFile will be in the path folder in the generated archive.
         *
         * @param path String, the relatif path with the root archive.
         * @param srcFile String, the absolute path of the file to add
         * @param zip ZipOutputStram, the stream to use to write the given file.
         */
        private static void addToZip(String path, String srcFile,
                                     ZipOutputStream zip) {

          File folder = new File(srcFile);
          if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
          }
          else {
    // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            try {
              FileInputStream in = new FileInputStream(srcFile);
              zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
              while ( (len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
              }
            }
            catch (Exception ex) {
              ex.printStackTrace();
            }
          }
        }

        /**
         * add the srcFolder to the zip stream.
         *
         * @param path String, the relatif path with the root archive.
         * @param srcFile String, the absolute path of the file to add
         * @param zip ZipOutputStram, the stream to use to write the given file.
         */
        private static void addFolderToZip(String path, String srcFolder,
                                           ZipOutputStream zip) {
          File folder = new File(srcFolder);
          String fileListe[] = folder.list();
          try {
            int i = 0;
            while (true) {
              addToZip(path + "/" + folder.getName(), srcFolder + "/" + fileListe[i],
                       zip);
              i++;
            }
          }
          catch (Exception ex) {
          }
        }

      }

    public class MainArgv {
      public static void main (String argv[]) {
        FolderZiper fz = new FolderZiper();
        fz.zipFolder("C:
    Documents and Settings\\User\\Desktop
    FolderZiper","FolderZiper.zip") ;


      }
    }
  2. Try closing the fileWriter object.