我在我的Android应用中使用NanoHTTPD作为Web服务器,希望在服务器端压缩一些文件并创建InputStream,然后使用Code A在客户端下载该InputStream。
我已经阅读了如何压缩和解压缩文件?上的Code B,但是如何在Android中直接创建ZIP InputStream而不先创建ZIP文件呢?
顺便说一句,我不认为Code C是一个好方法,因为它首先创建ZIP文件,然后将ZIP文件转换为FileInputStream,我希望能够直接创建ZIP InputStream!
Code A
private Response ActionDownloadSingleFile(InputStream fis) {
Response response = null;
response = newChunkedResponse(Response.Status.OK, "application/octet-stream",fis);
response.addHeader("Content-Disposition", "attachment; filename="+"my.zip");
return response;
}
代码B
public static void zip(String[] files, String zipFile) throws IOException {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try {
byte data[] = new byte[BUFFER_SIZE];
for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER_SIZE);
try {
ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
}
finally {
origin.close();
}
}
}
finally {
out.close();
}
}
代码 C
File file= new File("my.zip");
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
} catch (FileNotFoundException ex)
{
}
ZipOutputStream
,包装在服务器技术提供给你的任何输出流周围。 - user207421new ZipInputStream(new FileInputStream(zipFile))
。 - Jenna Sloan