Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class ZipUtil {
  2. private static void zipFiles(File targetDirectory, List<File> files) throws IOException {
  3. FileOutputStream fos = new FileOutputStream(targetDirectory.getPath() + ".zip");
  4. try{
  5. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  6. try{
  7. ZipOutputStream zos = new ZipOutputStream(baos);
  8. try{
  9. for (File file : files){
  10. byte[] content = FileUtils.readFileToByteArray(file);
  11. ZipEntry entry = new ZipEntry(file.getCanonicalPath().substring(targetDirectory.getCanonicalPath().length()+1,
  12. file.getCanonicalPath().length()));
  13. entry.setSize(content.length);
  14. zos.putNextEntry(entry);
  15. zos.write(content);
  16. zos.closeEntry();
  17. }
  18. }
  19. finally {
  20. zos.flush();
  21. zos.close();
  22. }
  23. baos.writeTo(fos);
  24. }
  25. finally {
  26. baos.flush();
  27. baos.close();
  28. }
  29. }
  30. finally {
  31. fos.flush();
  32. fos.close();
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement