2016-04-23 142 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