Java 中压缩文件的问题

发布于 2024-10-30 17:30:21 字数 1511 浏览 2 评论 0原文

我目前正在尝试压缩目录中的所有文件。

正在创建 zip 文件并且正在处理文件 - 但由于某种原因,文件没有出现在 zip 文件中。

用于完成此任务的代码如下:

public class FileZipper {

   public void zipDir( String dir, String zipFileName ) {
        try{
            File dirObj = new File(dir);
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
            Logger.info("Creating : " + zipFileName);
            addDir(dirObj, out);
            out.close();
        }
        catch (Exception e){
            Logger.error( e, "Error zipping directory" );
        }
  }

  private void addDir(File dirObj, ZipOutputStream out) throws IOException {
      File[] files;
      if( !dirObj.isDirectory() ){
          files = new File[] { dirObj };
      }
      else{
          files = dirObj.listFiles();
      }
      byte[] tmpBuf = new byte[1024];

      for (int i = 0; i < files.length; i++) {
          if (files[i].isDirectory()) {
              addDir(files[i], out);
              continue;
          }
          FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
          Logger.info(" Adding: " + files[i].getAbsolutePath());
          out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
          int len;
          while ((len = in.read(tmpBuf)) > 0) {
              out.write(tmpBuf, 0, len);
          }
          out.closeEntry();
          in.close();
      }
  }
}

在查看日志信息时,正在查找并处理目录中的文件,但创建的 zip 文件不包含任何数据。

任何有关此问题的帮助将不胜感激。

谢谢

I am currently trying to zip all files within a directory.

The zip file is being created and the files are being processed - but for some reason the files are not appearing within the zip file.

The code being used to complete this task is as follows:

public class FileZipper {

   public void zipDir( String dir, String zipFileName ) {
        try{
            File dirObj = new File(dir);
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
            Logger.info("Creating : " + zipFileName);
            addDir(dirObj, out);
            out.close();
        }
        catch (Exception e){
            Logger.error( e, "Error zipping directory" );
        }
  }

  private void addDir(File dirObj, ZipOutputStream out) throws IOException {
      File[] files;
      if( !dirObj.isDirectory() ){
          files = new File[] { dirObj };
      }
      else{
          files = dirObj.listFiles();
      }
      byte[] tmpBuf = new byte[1024];

      for (int i = 0; i < files.length; i++) {
          if (files[i].isDirectory()) {
              addDir(files[i], out);
              continue;
          }
          FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
          Logger.info(" Adding: " + files[i].getAbsolutePath());
          out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
          int len;
          while ((len = in.read(tmpBuf)) > 0) {
              out.write(tmpBuf, 0, len);
          }
          out.closeEntry();
          in.close();
      }
  }
}

When reviewing the logging information, the files within the directories are being found and processed, but the created zip file contains no data.

Any help with this issues will be greatly appreciated.

Thanks

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

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

发布评论

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

评论(2

清风疏影 2024-11-06 17:30:21

除了通过绝对路径添加文件可能不是您想要的这一事实之外,这段代码对我来说工作得很好。

Apart from the fact that adding the file by its absolute path is probably not what you want, this code works just fine for me.

酒绊 2024-11-06 17:30:21

嘿,
为该函数指定一组文件名和一个 zip 名称。
它应该有效。

private void zipFiles (ArrayList<String> listWithFiles, String zipName) {

        try {

            byte[] buffer = new byte[1024];

            // create object of FileOutputStream
            FileOutputStream fout = new FileOutputStream(zipName);

            // create object of ZipOutputStream from FileOutputStream
            ZipOutputStream zout = new ZipOutputStream(fout);

            for (String currentFile : listWithFiles) {

                // create object of FileInputStream for source file
                FileInputStream fin = new FileInputStream(currentFile);

                // add files to ZIP
                zout.putNextEntry(new ZipEntry(currentFile ));

                // write file content
                int length;

                while ((length = fin.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }

                zout.closeEntry();

                // close the InputStream
                fin.close();
            }

            // close the ZipOutputStream
            zout.close();
        } catch (IOException ioe) {
            System.out.println("IOException :" + ioe);
        }
    }

一切都对你好,

Hy,
Give a set of files name to this function, and a zip name.
It should work.

private void zipFiles (ArrayList<String> listWithFiles, String zipName) {

        try {

            byte[] buffer = new byte[1024];

            // create object of FileOutputStream
            FileOutputStream fout = new FileOutputStream(zipName);

            // create object of ZipOutputStream from FileOutputStream
            ZipOutputStream zout = new ZipOutputStream(fout);

            for (String currentFile : listWithFiles) {

                // create object of FileInputStream for source file
                FileInputStream fin = new FileInputStream(currentFile);

                // add files to ZIP
                zout.putNextEntry(new ZipEntry(currentFile ));

                // write file content
                int length;

                while ((length = fin.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }

                zout.closeEntry();

                // close the InputStream
                fin.close();
            }

            // close the ZipOutputStream
            zout.close();
        } catch (IOException ioe) {
            System.out.println("IOException :" + ioe);
        }
    }

All good to you,
dAN

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