2015-06-04 297 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

相关问题