在java中压缩和解压

发布于 2024-10-09 19:48:23 字数 2300 浏览 5 评论 0原文

我知道这是一项简单的任务,但是在更改我的代码后它停止工作并且我无法将其恢复!我使用两个函数来压缩和解压缩,即使它实际上做的是“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();
      }
   }

有任何帮助/建议吗?

创建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)

I know that it is an easy task, but after changing my code it stopped working and I can't get it back! I use two functions to zip and unzip, even though what it actually does is "jar" and "unjar", but that shouldn't make a huge difference

   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?

The error occurs when creating the 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-10-16 19:48:23

不知怎的,我让它工作了,显然它与流相关......感谢大家的帮助!

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);
       }
   }
}

Somehow I got it working, apparently it WAS stream-related... thanks for the help, everyone!

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);
       }
   }
}
原野 2024-10-16 19:48:23

我不知道这是否是您的问题,但通常来说,在完成写作后关闭每个 zip 条目是一个很好的做法。

请参阅 ZipOutputStream.closeEntry()

在您显示的代码中,zip 中的最后一个条目不会关闭。您也不会显示关闭 JarOutputStream 本身的位置。这可能会导致您创建无效的 zip 文件,当使用其他方法读回这些文件时,会出现错误。

I don't know if this is your problem or not, but it is generally good practice to close each zip entry after you finish writing.

See ZipOutputStream.closeEntry().

In the code that you show, the very last entry in the zip would not be closed. You also don't show where you close the JarOutputStream itself. This could be causing you to create invalid zip files, which would have errors when they are read back in using your other method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文