我使用FileSystem修改zip文件中的属性文件
public boolean processZip()
{
boolean wasModified = false;
Path zipFilePath = Paths.get(this.getZipFileName());
try (FileSystem fs = FileSystems.newFileSystem(zipFilePath, null))
{
Logger.info("Processing zip: #0",
this.getZipFileName());
Path source = fs.getPath(
"/some/path/some.properties");
// Delete old properties file.
Files.delete(source);
this.createPropertiesFile(source);
wasModified = true;
}
catch (NoSuchFileException e)
{
Logger.info("Properties file not found.");
}
catch (Exception e)
{
Logger.error("There was an error updating the properties in zip: " +
this.getZipFileName(),
e);
}
return wasModified;
}
protected void createPropertiesFile(Path dst) throws IOException
{
try (BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(Files.newOutputStream(dst))))
{
bw.write(this.getProperties().getValues());
}
catch (Exception e)
{
Logger.error("There was an error creating the properties in zip: " +
this.getInputJarFileName(),
e);
}
}
但是偶尔会出现以下堆栈跟踪的窗口失败:
ERROR: There was an error updating the properties in zip: C:\Apps\Test\some.zip
java.nio.file.FileSystemException: C:\Apps\Test\some.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:1294)
at com.sun.nio.zipfs.ZipFileSystem.close(ZipFileSystem.java:277)
问题是,该文件被先前的进程复制(该进程在修改zip文件时结束)。所以我不知道在Windows中可以使用该文件的内容。起初我认为可能是防病毒软件,但即使关闭防病毒软件也会发生这种情况。
我不确定是否还有其他措施可以防止这种情况发生。它不是那么频繁,但偶尔会发生。
它有管理员权限,我试过爆炸拉链,修改文件并将其压缩回来。这只会使问题更加频繁。我也尝试过Apache commons FileUtils.forceDelete(file)。
我有什么遗失的东西吗?