2017-02-20 161 views
11

我在Android應用程序使用NanoHTTPD作爲Web服務器,希望壓縮某些文件並創建在服務器端的InputStream,我使用代碼A.如何在不創建ZIP文件的情況下在Android中創建ZIP InputStream?

我在How to zip and unzip the files?閱讀代碼B下載客戶端InputStream的,但如何在不創建ZIP文件的情況下在Android中創建ZIP InputStream?我不認爲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) 
{ 

} 
+0

您需要在服務器中創建一個*輸出*流。一個'ZipOutputStream',圍繞着服務器技術給你的任何輸出流。 – EJP

+0

謝謝!對於EJP,你能給我一些示例代碼嗎? – HelloCW

+0

由於ZipInputSteam擴展了FilterInputStream,所以不能直接製作ZipInputSteam,所以它基本上起到了包裝類的作用,它爲流讀取zip文件提供了功能。你需要像'new ZipInputStream(new FileInputStream(zipFile))' –

回答

4

紫pInputStream按照文檔ZipInputStream

ZipInputStream是用於讀取ZIP文件格式的文件的輸入流過濾器。包括對壓縮和未壓縮條目的支持。

早些時候,我以一種不可能使用ZipInputStream的方式回答了這個問題。對不起。

但投資一段時間,我發現,有可能按下面的代碼

這是非常明顯的後因爲你是在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(); 
+0

@HelloCW讓我知道你是否嘗試過,因爲它工作正常,根據所需的輸出。然而,它在結束流方面需要稍微優化。 –