无法将文件写入zip

时间:2016-04-23 16:49:59

标签: java file 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();

}

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

1 个答案:

答案 0 :(得分:-1)

该代码运作正常:

<强> zip.java

group_level

我在Debian 9 Stretch,OpenJDK 8

下编译了它

然后我创建了一个示例txt文件:

<强> hello.txt的

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();

}
}
然后我编译了它:

Hello World

最后运行它:

javac zip.java

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

是否您对Hello World read/write没有权限