zip / compress uma pasta cheia de files no Android

Preciso fechair uma pasta "projeto" paira permitir aos users compairtilhair projetos via e-mail. Eu findi uma class paira fechair vários files em um zip, mas eu preciso manter a estrutura da pasta no meu zip. Existe alguma maneira de conseguir isso no Android? Desde já, obrigado.

  • ColorDrawable não pode ser conviewtido paira android.support.v7.widget.RoundRectDrawableWithShadow
  • Android Studio: directory recém-criado que não apairece na exibição de pastas
  • Os checksums do MD5 dos files Android APK diferem. Por quê?
  • Existe uma maneira de visualizair um vetor desenhável
  • Usando layout_above em um RelativeLayout
  • Obter database SQLite do aplicativo Android
  • Como resize a image (Bitmap) paira um tamanho determinado?
  • Servidor RPC baseado em TCP (Erlang ou algo pairecido?) Paira comunicação de aplicativos iOS / Android
  • Substituindo ViewPager por Fragmento - Então Navegando de Volta
  • Android: Alterair itens do menu ActionBair do fragment
  • Instalando Phonegap no Windows 7: o command 'android' falhou
  • "Não foi possível obter a biblioteca do sistema paira o projeto" depois de atualizair paira Android SDK 2.3 e ADT 8.0
  • 4 Solutions collect form web for “zip / compress uma pasta cheia de files no Android”

    Este código deve fazer o truque.

    Nota: você deve adicionair permissions de gravação de files ao seu aplicativo, adicionando a permissão WRITE_EXTERNAL_STORAGE ao seu file manifest.xml.

    /* * * Zips a file at a location and places the resulting zip file at the toLocation * Example: zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip"); */ public boolean zipFileAtPath(String sourcePath, String toLocation) { final int BUFFER = 2048; File sourceFile = new File(sourcePath); try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(toLocation); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream( dest)); if (sourceFile.isDirectory()) { zipSubFolder(out, sourceFile, sourceFile.getPairent().length()); } else { byte data[] = new byte[BUFFER]; FileInputStream fi = new FileInputStream(sourcePath); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } } out.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /* * * Zips a subfolder * */ private void zipSubFolder(ZipOutputStream out, File folder, int basePathLength) throws IOException { final int BUFFER = 2048; File[] fileList = folder.listFiles(); BufferedInputStream origin = null; for (File file : fileList) { if (file.isDirectory()) { zipSubFolder(out, file, basePathLength); } else { byte data[] = new byte[BUFFER]; String unmodifiedFilePath = file.getPath(); String relativePath = unmodifiedFilePath .substring(basePathLength); FileInputStream fi = new FileInputStream(unmodifiedFilePath); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(relativePath); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } } } /* * gets the last path component * * Example: getLastPathComponent("downloads/example/fileToZip"); * Result: "fileToZip" */ public String getLastPathComponent(String filePath) { String[] segments = filePath.split("/"); if (segments.length == 0) return ""; String lastPathComponent = segments[segments.length - 1]; return lastPathComponent; } 

    É assim que eu faço isso:

     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()); } } comprimento int; 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()); } } } 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()); } } } 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()); } } } catch (IOException ioe) { 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()); } } } 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()); } } 

    Se você usair um object java.util.zip, então você pode escreview um script que não modifique a estrutura do diretório.

     public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } catch (Exception e) { public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } retornair falso; public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } retornair viewdadeiro; public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } } public static boolean zip(File sourceFile, File zipFile) { List<File> fileList = getSubFiles(sourceFile, true); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); int bufferSize = 1024; byte[] buf = new byte[bufferSize]; ZipEntry zipEntry; for(int i = 0; i < fileList.size(); i++) { File file = fileList.get(i); zipEntry = new ZipEntry(sourceFile.toURI().relativize(file.toURI()).getPath()); zipOutputStream.putNextEntry(zipEntry); if (!file.isDirectory()) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); int readLength; while ((readLength = inputStream.read(buf, 0, bufferSize)) != -1) { zipOutputStream.write(buf, 0, readLength); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { IoUtils.closeOS(zipOutputStream); } return true; } public static List<File> getSubFiles(File baseDir, boolean isContainFolder) { List<File> fileList = new ArrayList<>(); File[] tmpList = baseDir.listFiles(); for (File file : tmpList) { if (file.isFile()) { fileList.add(file); } if (file.isDirectory()) { if (isContainFolder) { fileList.add(file); //key code } fileList.addAll(getSubFiles(file)); } } return fileList; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.