添加和创建zip文件时出现Java错误:该进程无法访问该文件

问题描述

我使用Java ZipFileSystem(Java 8)制作了一个Java程序,用于将文件归档为zip文件。我以为它会简单明了,但是运行了一段时间后,它就显示了“ java.nio.file.FileSystemException”。下面的代码段:

        ......
        fileName = fileName + "_";
        int zipFileNameMidPart;
        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(sourceDir,getFilter(sourceDir)))
        {
          for(Path path : dirStream)
          {
            lastModified.setTimeInMillis(Files.getLastModifiedTime(path,LinkOption.NOFOLLOW_LINKS).toMillis());
            zipFileNameMidPart = fileName.equalsIgnoreCase("salesfaxes") ? lastModified.get(Calendar.MONTH)+1 : (lastModified.get(Calendar.MONTH)/3)+1;
            zipFileName = fileName+lastModified.get(Calendar.YEAR)+"_"+zipFileNameMidPart+".zip";
            zipFile = Paths.get(srcDirTmp,zipFileName);
            addToZipArchive(path,zipFile,!Files.exists(zipFile),tmpMoveDir);
            
            //Move archived file to the temp directory.
            currentBackupFile = tmpMoveDir.resolve(path.getFileName());
            logger.info("Moving "+path+" to "+currentBackupFile);
            Files.move(path,currentBackupFile,StandardCopyOption.REPLACE_EXISTING);
          }
        }//try
  
protected void addToZipArchive(Path fileToBeArchived,Path zipArchive,boolean create,Path tmpBackupDir) throws Exception
      {
        /* Define ZIP File System Properies in HashMap */    
        Map<String,String> zip_properties = new HashMap<>(); 
        /* set create to true if you want to create a new ZIP file */
        zip_properties.put("create",Boolean.toString(create));
        /* specify encoding to UTF-8 */
        zip_properties.put("encoding","UTF-8");        
        /* Locate File on disk for creation */
        URI zip_disk = URI.create("jar:file:/" + zipArchive.toString().replace("\\","/"));
        totalCnt++;
        /* Create ZIP file System */
        try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk,zip_properties)) 
        {
          /* Path inside ZIP File */
          Path pathInZipfile = zipfs.getPath(fileToBeArchived.getFileName().toString());
          boolean copyOk = true;
          try
          {
            logger.info("Adding "+fileToBeArchived.toString()+" to "+(create?"new ":"existing ")+"zip file "+zipArchive.toString());
                /* Add file to archive */
                Files.copy(fileToBeArchived,pathInZipfile,StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING); 
          }
          catch(Exception e)
          {
                copyOk = false;
            logger.log(Level.SEVERE,e.getMessage(),e);
          }
          if(copyOk)
          {         
            if(create)
              createCnt++;
            else
              addCnt++;    
          }
          else
          {
            logger.severe("Skip file "+fileToBeArchived+"! Error encounted while archiving it.");
            skippedCnt++;
          }
        }
      }

Stacktrace:

SEVERE: Error occured!
java.nio.file.FileSystemException: C:\temp\bols\bols_2017_4.zip: The process cannot access the file because it is being used by another process.
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1126)
at com.sun.nio.zipfs.ZipFileSystem.sync(ZipFileSystem.java:1301)
at com.sun.nio.zipfs.ZipFileSystem.close(ZipFileSystem.java:277)
at ZipArchiveUtil.addToZipArchive(ZipArchiveUtil.java:177)
at ZipArchiveUtil.processDirs(ZipArchiveUtil.java:282)
at ZipArchiveUtil.main(ZipArchiveUtil.java:392)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)

目录很大,包含成千上万个tif文件。 zip文件也可能变得非常大(文件大小超过了千兆字节)。 我试图在晚上和周末运行该程序,但运行一段时间后仍然遇到相同的错误。 还将环境复制到另一台PC上,结果相同。 似乎程序在尝试向其中添加新文件时仍保留着zip文件...我缺少什么?做了一些谷歌搜索,没有找到太多信息。

另一个奇怪的事情,程序不时“暂停”,什么也不做。如果执行CTRL Z,则它将再次开始处理。我在任务管理器中处于“暂停”状态时没有检查CPU和磁盘的使用情况。

任何帮助/建议都将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)