我正在使用TrueZip创建档案。似乎它将路径D:\
视为系统根文件夹并阻止我在那里创建文件。我的操作系统和所有程序默认位于C:
. 该驱动器D:\
包含个人数据(文档、图片、视频等)。
如何在驱动器的根目录创建档案?
我用来创建档案的代码:
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileOutputStream;
import de.schlichtherle.truezip.zip.ZipEntry;
import de.schlichtherle.truezip.zip.ZipOutputStream;
import javax.swing.*;
import java.io.*;
public class MainClass {
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Please select the target folder.");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
JFrame applicationFrame = new JFrame();
if (chooser.showSaveDialog(applicationFrame) != JFileChooser.APPROVE_OPTION)
return;
File selectedFile = chooser.getSelectedFile();
TFile archiveFile = new TFile(selectedFile + ".zip");
System.out.println("Selected file: " + archiveFile.getAbsolutePath());
ZipOutputStream zipOutputStream = new ZipOutputStream(new TFileOutputStream(archiveFile));
File folderToZip = new File("D:/filesToZip");
File[] files = folderToZip.listFiles();
for (File file : files) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int avail = bis.available();
byte[] buffer = new byte[avail];
if (avail > 0) {
bis.read(buffer, 0, avail);
}
bis.close();
ZipEntry zipEntry = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(buffer, 0, buffer.length);
zipOutputStream.closeEntry();
}
zipOutputStream.close();
System.exit(0);
}
}
我收到的错误消息:
Selected file: D:\test.zip
Exception in thread "main" java.io.FileNotFoundException: D:\test.zip
at de.schlichtherle.truezip.file.TFileOutputStream.newOutputStream(TFileOutputStream.java:147)
at de.schlichtherle.truezip.file.TFileOutputStream.<init>(TFileOutputStream.java:116)
at MainClass.main(MainClass.java:21)
Caused by: de.schlichtherle.truezip.fs.FsArchiveFileSystemException: <file system root> (only files can get replaced)
at de.schlichtherle.truezip.fs.FsArchiveFileSystem.mknod(FsArchiveFileSystem.java:379)
at de.schlichtherle.truezip.fs.FsBasicArchiveController$1Output.mknod(FsBasicArchiveController.java:273)
at de.schlichtherle.truezip.fs.FsBasicArchiveController$1Output.newOutputStream(FsBasicArchiveController.java:233)
at de.schlichtherle.truezip.fs.FsContextController$Output.newOutputStream(FsContextController.java:322)
at de.schlichtherle.truezip.fs.FsResourceController$Output.newOutputStream(FsResourceController.java:283)
at de.schlichtherle.truezip.socket.DelegatingOutputSocket.newOutputStream(DelegatingOutputSocket.java:57)
at de.schlichtherle.truezip.fs.FsSyncController$Output.newOutputStream(FsSyncController.java:454)
at de.schlichtherle.truezip.fs.FsLockController$Output$1NewOutputStream.call(FsLockController.java:509)
at de.schlichtherle.truezip.fs.FsLockController$Output$1NewOutputStream.call(FsLockController.java:506)
at de.schlichtherle.truezip.fs.FsLockController.locked(FsLockController.java:328)
at de.schlichtherle.truezip.fs.FsLockController.writeLocked(FsLockController.java:268)
at de.schlichtherle.truezip.fs.FsLockController$Output.newOutputStream(FsLockController.java:513)
at de.schlichtherle.truezip.fs.FsFinalizeController$Output.newOutputStream(FsFinalizeController.java:209)
at de.schlichtherle.truezip.fs.FsFalsePositiveArchiveController$1Output$NewOutputStream.call(FsFalsePositiveArchiveController.java:409)
at de.schlichtherle.truezip.fs.FsFalsePositiveArchiveController$1Output$NewOutputStream.call(FsFalsePositiveArchiveController.java:402)
at de.schlichtherle.truezip.fs.FsFalsePositiveArchiveController$TryChild.call(FsFalsePositiveArchiveController.java:507)
at de.schlichtherle.truezip.fs.FsFalsePositiveArchiveController.call(FsFalsePositiveArchiveController.java:104)
at de.schlichtherle.truezip.fs.FsFalsePositiveArchiveController$1Output.newOutputStream(FsFalsePositiveArchiveController.java:399)
at de.schlichtherle.truezip.file.TFileOutputStream.newOutputStream(TFileOutputStream.java:143)
... 2 more