Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ZipUtil {
- private static void zipFiles(File targetDirectory, List<File> files) throws IOException {
- FileOutputStream fos = new FileOutputStream(targetDirectory.getPath() + ".zip");
- try{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try{
- ZipOutputStream zos = new ZipOutputStream(baos);
- try{
- for (File file : files){
- byte[] content = FileUtils.readFileToByteArray(file);
- ZipEntry entry = new ZipEntry(file.getCanonicalPath().substring(targetDirectory.getCanonicalPath().length()+1,
- file.getCanonicalPath().length()));
- entry.setSize(content.length);
- zos.putNextEntry(entry);
- zos.write(content);
- zos.closeEntry();
- }
- }
- finally {
- zos.flush();
- zos.close();
- }
- baos.writeTo(fos);
- }
- finally {
- baos.flush();
- baos.close();
- }
- }
- finally {
- fos.flush();
- fos.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement