如何在Android中创建ZIP输入流而不先创建ZIP文件?
2022-09-03 15:45:55
我在我的Android APP中使用NanoHTTPD作为Web服务器,我希望压缩一些文件并在服务器端创建一个InputStream,并且我使用代码A在客户端下载InputStream。
我已经阅读了代码B如何压缩和解压缩文件?,但是如何在Android中创建ZIP输入流而不先创建ZIP文件?
顺便说一句,我不认为Code C是好方法,因为它首先制作ZIP文件,然后将ZIP文件转换为FileInputStream,我希望直接创建ZIP InputStream!
代码 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)
{
}