Advertisement
Guest User

Untitled

a guest
Feb 15th, 2012
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1.     public static void addFilesToZip(File source, File[] files){
  2.         try{
  3.             File tmpZip = File.createTempFile(source.getName(), null);
  4.             tmpZip.delete();
  5.             if(!source.renameTo(tmpZip)){
  6.                 throw new Exception("Could not make temp file (" + source.getName() + ")");
  7.             }
  8.             byte[] buffer = new byte[1024];
  9.             ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
  10.             ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
  11.             for(int i = 0; i < files.length; i++){
  12.                 InputStream in = new FileInputStream(files[i]);
  13.                 out.putNextEntry(new ZipEntry(files[i].getName()));
  14.                 for(int read = in.read(buffer); read > -1; read = in.read(buffer)){
  15.                     out.write(buffer, 0, read);
  16.                 }
  17.                 out.closeEntry();
  18.                 in.close();
  19.             }
  20.             for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){
  21.                 out.putNextEntry(ze);
  22.                 for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){
  23.                     out.write(buffer, 0, read);
  24.                 }
  25.                 out.closeEntry();
  26.             }
  27.             out.close();
  28.             tmpZip.delete();
  29.         }catch(Exception e){
  30.             e.printStackTrace();
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement