[英]Why is my file doesn't read to a zip archive
I have a code snippet, that in theory should read a path to an archive first, when write a file to the archive. 我有一个代码段,理论上,当将文件写入存档时,应该首先读取存档的路径。 (But that thing takes zip and for instance some txt file, and really move it to zip, but the file is empty) First, i thought this thing doesn't work since i didn't close streams, but now i use try-with, so the problem should be gone, but it is not.
(但是那东西需要zip和例如一些txt文件,然后将其真正移动到zip中,但是文件为空)首先,我认为这件事不起作用,因为我没有关闭流,但是现在我使用try-因此,问题应该消失了,但事实并非如此。
public void createZip(Path source) throws Exception
{
try(ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(source));
ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile)))
{
ZipEntry zipEntry = new ZipEntry(source.getFileName().toString());
zipOut.putNextEntry(zipEntry);
int data;
while((data = zipIn.read()) > 0)
{
zipOut.write(data);
}
}
}
To use correctly the ZipOutputStream after adding the entry you need to flush the zip. 要在添加条目后正确使用ZipOutputStream,需要刷新zip。 Try adding the following at the endo of your method:
尝试在方法的末尾添加以下内容:
zipOut.flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.