我尝试将一些文件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();
}
存档已创建,但我看不到其中的任何文件。为什么呢?
答案 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
没有权限?