繁体   English   中英

无法将文件写入zip

[英]Can't write file to zip

我尝试将一些文件fileNamePath放在zip存档中(参数为D:\\ text.txt D:\\ archive.zip):

  public static void main(String[] args) throws IOException {
    if (args.length==0) return;

    String fileNamePath = args[0];
    String zipPath = args[1];

    FileOutputStream outputStream = new FileOutputStream(zipPath);
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    zipOutputStream.putNextEntry(new ZipEntry(fileNamePath));

    File file = new File(fileNamePath);
    Files.copy(file.toPath(),zipOutputStream);

    zipOutputStream.closeEntry();
    zipOutputStream.close();

}

存档已创建,但我看不到任何文件。 为什么?

该代码运行完美:

压缩文件

import java.io.*;
import java.nio.file.*;
import java.util.zip.*;

public class zip
{
public static void main(String[] args) throws IOException {
    if (args.length==0) return;

    String fileNamePath = args[0];
    String zipPath = args[1];

    FileOutputStream outputStream = new FileOutputStream(zipPath);
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    zipOutputStream.putNextEntry(new ZipEntry(fileNamePath));

    File file = new File(fileNamePath);
    Files.copy(file.toPath(),zipOutputStream);

    zipOutputStream.closeEntry();
    zipOutputStream.close();

}
}

我已经在Debian 9 Stretch,OpenJDK 8下编译了它

然后,我创建了一个样本txt文件:

hello.txt

Hello World

然后,我对其进行了编译:

javac zip.java

最后运行它:

java zip hello.txt hello.zip

我解压缩.zip并打开hello.txt,返回Hello World

可能是您没有read/write D:\\ 权限

暂无
暂无

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

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