2015-06-04 401 views
1

我已經實現Zip文件夾或文件,然後下載並保存到內存。 我的問題是,它無需下載任何錯誤,但我沒有得到Zip文件,如果我點擊下載的文件,它顯示:文件夾是文件不是壓縮

無法顯示

或者

無法顯示消息。

我的代碼:

String fileName = tvtitle.getText().toString(); 
     String fileExtension = tvtype.getText().toString(); 
     File imageDirectory = new File(Path); 
     imageDirectory.mkdirs(); 
     int fileLength = connection.getContentLength(); 
     String _path = Path; 
     input = connection.getInputStream(); 
     File outputFile = new File(_path, fileName + fileExtension); 
     FileOutputStream fos = new FileOutputStream(outputFile); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     File srcFile = new File(input.toString()); 
     byte[] buffer = new byte[1024]; 
     zos.putNextEntry(new ZipEntry(srcFile.getName())); 
     int length; 
     while ((length = input.read(buffer)) > 0) { 

      zos.write(buffer, 0, length); 

     } 
     zos.closeEntry(); 
     input.close(); 
     zos.close(); 
+1

「我沒有得到Zip文件,如果我點擊下載的文件」 - 如果你沒有收到文件,那你怎麼點擊它? – immibis

+0

使用zip4j其方便,快捷... http://www.lingala.net/zip4j/download.php –

+0

對不起Zip文件我沒有得到,我得到一個文件沒有zip,如果我點擊該文件,它是表現如此。 – Piku

回答

1

我只是不知道這是什麼變量「輸入

我想你應該有的FileInputStream(讀取源文件)的類型與FileOutputStream配對(設置爲目標/ zip文件)。

而此行的代碼:

File outputFile = new File(_path, fileName + fileExtension); 

你的輸出必須是.zip文件吧? 所以,它應該是:

File outputFile = new File(_path, fileName + ".zip"); 

或類似的東西

這將是這樣

String fileName = tvtitle.getText().toString(); 
String fileExtension = tvtype.getText().toString(); 
File imageDirectory = new File(Path); 
FileInputStream fis = new FileInputStream(imageDirectory); 
ZipEntry zipEntry = new ZipEntry(fileName); 

FileOutputStream fos = new FileOutputStream("test.zip"); 
ZipOutputStream zos = new ZipOutputStream(fos); 
zos.putNextEntry(zipEntry); 
byte[] bytes = new byte[1024]; 
int length; 
    while ((length = fis.read(bytes)) >= 0) { 
     zos.write(bytes, 0, length); 
    } 
zos.closeEntry(); 
fis.close(); 
zos.close(); 
fos.close(); 
+0

不,它不工作 – Piku

+0

這是拋出FileNotFound異常 – Piku

+0

在「輸入」我通過的網址 – Piku

相關問題