2015-06-03 2 views
0

Я реализовал загрузку файлов. Он загружает и сохраняет его в памяти.Как зашифровать папки и загрузить

Теперь моя проблема в том, что если есть папка и подпапка, мне нужно ZIP-папку и загрузить zip-файл и сохранить его в памяти. Я пробовал много, но я не нашел решения. Может ли кто-нибудь помочь мне получить решение?

Здесь мой скачать единый код файл ...

@Override 
    protected String doInBackground(String... sUrl) { 
     InputStream input = null; 
     OutputStream output = null; 
     ZipOutputStream zos = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " 
         + connection.getResponseCode() + " " 
         + connection.getResponseMessage(); 
      } 

      String fileName = tvtitle.getText().toString(); 
      String fileExtension = tvtype.getText().toString(); 

      File imageDirectory = new File(Path); 
      imageDirectory.mkdirs(); 
      int fileLength = connection.getContentLength(); 
      String _path = Path; 
      input = connection.getInputStream(); 
      File outputFile = new File(_path, fileName + fileExtension); 
      output = new FileOutputStream(outputFile); 
      // zos = new ZipOutputStream(output); 

      byte data[] = new byte[4096]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 

       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 
       total += count; 

       if (fileLength > 0) // only if total length is known 
        publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (output != null) 
        output.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 
+0

На самом деле вы должны написать код для zipping на сервере. Android просто загрузит все, что предоставляется. – Paritosh

+0

есть идентификатор метода ZipOutputStream, но я dnt knw, как его использовать – Piku

ответ

0

первое разрешение добавить в очевидном

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

называют этот метод, передавая "inputFolderPath" и "outputFolderPath"

private static void zipFolder(String inputFolderPath, String outZipPath) { 
try { 
    FileOutputStream fos = new FileOutputStream(outZipPath); 
    ZipOutputStream zos = new ZipOutputStream(fos); 
    File srcFile = new File(inputFolderPath); 
    File[] files = srcFile.listFiles(); 
    Log.d("", "Zip directory: " + srcFile.getName()); 
    for (int i = 0; i < files.length; i++) { 
     Log.d("", "Adding file: " + files[i].getName()); 
     byte[] buffer = new byte[1024]; 
     FileInputStream fis = new FileInputStream(files[i]); 
     zos.putNextEntry(new ZipEntry(files[i].getName())); 
     int length; 
     while ((length = fis.read(buffer)) > 0) { 
      zos.write(buffer, 0, length); 
     } 
     zos.closeEntry(); 
     fis.close(); 
    } 
    zos.close(); 
} catch (IOException ioe) { 
    Log.e("", ioe.getMessage()); 
} 
} 
+0

Можете ли вы обновить мой код выше ...? – Piku

+0

Этот код немного путается – Piku

+0

Возможно, вы можете обновить – Piku

Смежные вопросы