Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. package zipowanie;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8.  
  9. /**
  10.  * Program zapisuje wiele plików do jednego archiwum zip
  11.  */
  12. public class JedenZipWielePlikowZipowanie
  13. {
  14.     public static void main(String[] argumenty)
  15.     {
  16.         int i = 0;
  17.        
  18.         try(FileOutputStream fos = new FileOutputStream("zip_wiele_plikow.zip"))
  19.         {
  20.             try(ZipOutputStream zos = new ZipOutputStream(fos))
  21.             {
  22.                 for(i = 0; i<5; i++)
  23.                 {
  24.                     try(FileInputStream fis = new FileInputStream("anime1.jpg"))
  25.                     {
  26.                         ZipEntry ze = new ZipEntry(Integer.toString(i) + ".jpg");
  27.                         zos.putNextEntry(ze);
  28.  
  29.                         byte[] buffer = new byte[1024];
  30.                         int c;
  31.                         while(( c = fis.read(buffer, 0, 1024)) != -1)
  32.                         {
  33.                             zos.write(buffer, 0, c);
  34.                         }
  35.  
  36.                         zos.closeEntry();
  37.                         System.out.println("Zapis pliku " + Integer.toString(i) + ".jpg zakonczony!");
  38.  
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.         catch(IOException ex)
  44.         {
  45.             System.out.println("Nie znaleziono pliku!");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement