简体   繁体   English

Java中的压缩文件问题

[英]Issues with zipping files in Java

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. 正在创建zip文件并正在处理文件-但由于某些原因,文件未出现在zip文件中。

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. 查看日志记录信息时,将找到并处理目录中的文件,但是创建的zip文件不包含任何数据。

Any help with this issues will be greatly appreciated. 任何与此问题的帮助将不胜感激。

Thanks 谢谢

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

Hy, Give a set of files name to this function, and a zip name. 嗨,为该功能指定一组文件名和一个zip名称。 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 对您有好处,dAN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM