2016-04-23 143 views
0

我试图把文件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(); 

} 

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

+2

[将文件添加到ZIP文件]可能的重复(http://stackoverflow.com/questions/10103861/adding-files-to-zip-file) – aribeiro

+0

您没有看到任何文件?当我用相同的参数运行上面的代码时它创建了一个带有文件夹D的zip文件:它内部和text.txt被放置。 –

+0

嗯,这有点好笑。当我解压档案文件时。但是当我双击zip时没有看到它。我使用win7 – SergeiK

回答

-1

即码被正常使用:

zip.java

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弹力编译它,OpenJDK的8

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

hello.txt

Hello World 

我然后编译它:

javac zip.java 

最后运行:

java zip hello.txt hello.zip 

我解压.zip和开拓hello.txt的,返回Hello World

愿它是你没有权限read/writeD:\

+0

我确实拥有所有权限 – SergeiK