2017-02-20 1 views
11

I NanoHTTPD als Web-Server in meinem Android APP verwenden, ich hoffe, einige Dateien zu komprimieren und einen Inputstream in Server-Seite zu erstellen, und ich lade Sie die Inputstream in Client-Seite-Code A.Wie erstellt man einen ZIP InputStream in Android, ohne zuerst eine ZIP-Datei zu erstellen?

mit

I Code B bei How to zip and unzip the files? gelesen , aber wie erstellt man einen ZIP InputStream in Android, ohne zuerst eine ZIP-Datei zu erstellen?

BTW, ich glaube nicht, Code C ist gut Weg, weil es ZIP-Datei zuerst machen, dann konvertieren ZIP-Datei zu FileInputStream, ich hoffe, einen ZIP-EingangStream direkt zu erstellen!

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; 
} 

Code 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

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

} 
+0

Sie müssen einen * Ausgang * -Stream auf dem Server erstellen. Ein 'ZipOutputStream', um den Ausgabestrom gewickelt, den die Servertechnologie Ihnen gibt. – EJP

+0

Danke! Könnten Sie mir einen Beispielcode für EJP geben? – HelloCW

+0

Sie können ZipInputSteam nicht direkt erstellen, da ZipInputSteam FilterInputStream erweitert, sodass es im Grunde genommen als Wrapper-Klasse fungiert, die Funktionen für den Stream zum Lesen von ZIP-Dateien bereitstellt. Du brauchst etwas wie 'new ZipInputStream (neuer FileInputStream (zipFile))' –

Antwort

4

Zi pInputStream gemäß der Dokumentation ZipInputStream

ZipInputStream ist ein Eingangsstrom-Filter für Dateien in der ZIP-Datei-Format zu lesen. Beinhaltet Unterstützung für komprimierte und unkomprimierte Einträge.

Früher habe ich auf diese Frage so geantwortet, dass es nicht möglich ist, ZipInputStream zu verwenden. Es tut mir leid.

aber nach einiger Zeit zu investieren Ich fand, dass es gemäß dem folgenden Code möglich ist

Es ist sehr offensichtlich, dass Sie Dateien im Zip-Format über das Netzwerk senden, da .

//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 lassen Sie mich wissen, wenn Sie es versucht haben, weil es gut funktioniert, wie für die gewünschte Ausgabe. Es bedarf jedoch einer kleinen Optimierung hinsichtlich des Closing-Streams. –

Verwandte Themen