简体   繁体   English

如何在Android中创建ZIP InputStream而不先创建ZIP文件?

[英]How to create a ZIP InputStream in Android without creating a ZIP file first?

I use NanoHTTPD as web server in my Android APP, I hope to compress some files and create a InputStream in server side, and I download the InputStream in client side using Code A. 我在我的Android APP中使用NanoHTTPD作为Web服务器,我希望在服务器端压缩一些文件并创建一个InputStream,然后使用Code A在客户端下载InputStream。

I have read Code B at How to zip and unzip the files? 我在如何压缩和解压缩文件中读过代码B , but how to create a ZIP InputStream in Android without creating a ZIP file first? ,但如何在Android中创建ZIP InputStream而不首先创建ZIP文件?

BTW, I don't think Code C is good way, because it make ZIP file first, then convert ZIP file to FileInputStream , I hope to create a ZIP InputStream directly! 顺便说一句,我不认为Code C是好方法,因为它先制作ZIP文件,然后将ZIP文件转换为FileInputStream,我希望直接创建一个ZIP InputStream!

Code A 代码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;
}

Code B 代码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();
    }
}

Code C 代码C.

File file= new File("my.zip");
FileInputStream fis = null;
try
{
    fis = new FileInputStream(file);
} catch (FileNotFoundException ex)
{

}

ZipInputStream as per the documentation ZipInputStream ZipInputStream根据文档ZipInputStream

ZipInputStream is an input stream filter for reading files in the ZIP file format. ZipInputStream是一个输入流过滤器,用于读取ZIP文件格式的文件。 Includes support for both compressed and uncompressed entries. 包括对压缩和未压缩条目的支持。

Earlier I answered to this question in a way that it is not possible using ZipInputStream . 之前我以一种不可能使用ZipInputStream的方式回答了这个问题。 I am Sorry. 对不起。

But after investing some time I found that it is possible as per the below code 但经过一段时间的投入,我发现按照以下代码可以实现

It is very much obvious that since you are sending files in zip format over the network. 很明显,因为您通过网络以zip格式发送文件。

//Create proper background thread pool. Not best but just for solution
new Thread(new Runnable() {
  @Override
  public void run() {

   // Moves the current Thread into the background
   android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

    HttpURLConnection httpURLConnection = null;
    byte[] buffer = new byte[2048];
    try {
      //Your http connection
      httpURLConnection = (HttpURLConnection) new URL("https://s3-ap-southeast-1.amazonaws.com/uploads-ap.hipchat.com/107225/1251522/SFSCjI8ZRB7FjV9/zvsd.zip").openConnection();

      //Change below path to Environment.getExternalStorageDirectory() or something of your
      // own by creating storage utils
      File outputFilePath = new File  ("/mnt/sdcard/Android/data/somedirectory/");

      ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(httpURLConnection.getInputStream()));
      ZipEntry zipEntry = zipInputStream.getNextEntry();

      int readLength;

      while(zipEntry != null){
        File newFile = new File(outputFilePath, zipEntry.getName());

        if (!zipEntry.isDirectory()) {
          FileOutputStream fos = new FileOutputStream(newFile);
          while ((readLength = zipInputStream.read(buffer)) > 0) {
            fos.write(buffer, 0, readLength);
          }
          fos.close();
        } else {
          newFile.mkdirs();
        }

        Log.i("zip file path = ", newFile.getPath());
        zipInputStream.closeEntry();
        zipEntry = zipInputStream.getNextEntry();
      }
      // Close Stream and disconnect HTTP connection. Move to finally
      zipInputStream.closeEntry();
      zipInputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      // Close Stream and disconnect HTTP connection.
      if (httpURLConnection != null) {
        httpURLConnection.disconnect();
      }
    }
  }
}).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM