开发者

Zipping and Unzipping Jar, It's not the same size!

I'm making a modding application for a game. When I unzip and zip the same files: for some reason although it is open-able with 7Zip, it's not open-able in the actual application. Even though I've not edited the unzipped files - it's missing a few bytes. I gathered the methods from the net and have been editing and retrying for the last 5 hours without success, any help guys? Thanks!

Here's my code:

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, Stri开发者_开发知识库ng basefolder, JarOutputStream jos) throws Exception
   {
        FileInputStream fis = null;
       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);

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

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


Without debugging your code I can say that the most probable reason is the compression level. The default compression level is Deflater.DEFAULT_COMPRESSION. You can change it using ZipOutputStream.setLevel().

I believe that you created your original jar file using jar utility or some other zip creator. Then you created the same using your code and got different size. In this case the compression level may explain the difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜