English 中文(简体)
j
原标题:zip and unzip in java

我知道,这是一项轻松的任务,但在修改我的法典之后,它停止了工作,我可以把它带回来! 我利用两项职能来推敲和 un击,尽管它的实际作用是“jar”和“unjar”,但这不应带来巨大变化。

   public static void zipit(File[] infiles, JarOutputStream jos) throws Exception
   {
      zipit(infiles,"", jos);
   }

   public static void zipit(File[] infiles, String root, JarOutputStream jos) throws Exception
   {

      byte[] buffer = new byte[4096];

      for(int i=0; i<infiles.length; i++)
      {
         // recursive call for subfolders... temporary
         if(infiles[i].isDirectory())
         {
            zipit(infiles[i].listFiles(), infiles[i].getName() + "/", jos);
            continue;
         }

         // create string with absolute path
         String entryfile = root + infiles[i].getName();

         JarEntry entry = new JarEntry(entryfile);
         zos.putNextEntry(entry);

         FileInputStream fis = new FileInputStream(infiles[i]);

         int count;
         while((count = fis.read(buffer, 0, buffer.length)) != -1)
            zos.write(buffer, 0, count);
      }
   }

   public static void unzipit(File zipfile, File outputfolder) throws Exception
   {
      JarFile jar = new JarFile(zipfile);

      for(Enumeration entries = jar.entries(); entries.hasMoreElements(); )
      {
         JarEntry entry = (JarEntry) entries.nextElement();
         File unzipped = new File(outputfolder, entry.getName());
         if (entry.isDirectory() && !unzipped.exists())
         {
            unzipped.mkdirs();
            continue;
         }
         else if (!unzipped.getParentFile().exists())
            unzipped.getParentFile().mkdirs();

         FileOutputStream fos = new FileOutputStream(unzipped);
         InputStream in = jar.getInputStream(entry);

         byte[] buffer = new byte[4096];
         int count;
         while((count = in.read(buffer, 0, buffer.length)) != -1)
            fos.write(buffer, 0, count);
         fos.close();
      }
   }

Any help/suggestions?

错误是在造成JarFile时发生的:

java.util.zip.ZipException: error in opening zip file
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:131)
    at java.util.jar.JarFile.<init>(JarFile.java:150)
    at java.util.jar.JarFile.<init>(JarFile.java:114)
最佳回答
问题回答

有些人说,我去做工作,这显然是与西非经货共同体上游有关的。 感谢大家的帮助!

class zipper
{
     static byte[] buffer = new byte[4096];

     public static void unzip(File zipfile, File outputfolder)  throws Exception
     {
         JarFile zip = new JarFile(zipfile);

         Enumeration entries = zip.entries();
         while(entries.hasMoreElements())
         {
             JarEntry entry = (JarEntry) entries.nextElement();
             File unzipped = new File(outputfolder,entry.getName());

             if (entry.isDirectory() && !unzipped.exists())
             {
                 unzipped.mkdirs();
                 continue;
             }
             else if (!unzipped.getParentFile().exists())
                 unzipped.getParentFile().mkdirs();

             InputStream in = zip.getInputStream(entry);
             FileOutputStream fos = new FileOutputStream(unzipped);

             int count;
             while((count = in.read(buffer, 0, buffer.length)) != -1)
                 fos.write(buffer, 0, count);

             // clean up
             fos.close();
             in.close();
         }
   }

   public static void zip(File[] infiles, JarOutputStream jos) throws Exception
   {
       zip(infiles,"",jos);

       // clean up
       jos.flush();
       jos.close();
   }

   public static void zip(File[] infiles, String basefolder, JarOutputStream jos) throws Exception
   {
       for(int i=0; i<infiles.length; i++)
       {
           if(infiles[i].isDirectory())
           {
               // recursive call for directories
               zip(infiles[i].listFiles(), infiles[i].getName() + File.separator, jos);
               continue;
           }

           String filepath = basefolder + infiles[i].getName();
           JarEntry entry = new JarEntry(filepath);
           jos.putNextEntry(entry);

           FileInputStream fis = new FileInputStream(infiles[i]); // get stream

           int count;
           while((count = fis.read(buffer, 0, buffer.length)) != -1)
               jos.write(buffer, 0, count);
       }
   }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签