Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package zip;
  2.  
  3. import java.io.*;
  4. import java.nio.file.*;
  5. import java.util.zip.*;
  6.  
  7. /**
  8. * This Java program demonstrates how to compress a file in ZIP format.
  9. *
  10. * @author www.codejava.net
  11. */
  12. public class zip {
  13.  
  14. private static void zipFile(String filePath) {
  15. try {
  16. filePath = "C:\\Users\\Frage\\Desktop\\FragerPT\\JQlimax";
  17.  
  18. File file = new File(filePath);
  19. // String zipFileName = file.getName().concat(".zip");
  20. String zipFileName="C:\\Users\\Frage\\Desktop\\FragerPT\\cenas1.zip";
  21. FileOutputStream fos = new FileOutputStream(zipFileName);
  22. ZipOutputStream zos = new ZipOutputStream(fos);
  23.  
  24. zos.putNextEntry(new ZipEntry(file.getName()));
  25.  
  26. byte[] bytes = Files.readAllBytes(Paths.get(filePath));
  27. zos.write(bytes, 0, bytes.length);
  28. zos.closeEntry();
  29. zos.close();
  30.  
  31. } catch (FileNotFoundException ex) {
  32. System.err.format("The file %s does not exist", filePath);
  33. } catch (IOException ex) {
  34. System.err.println("I/O error: " + ex);
  35. }
  36. }
  37.  
  38. public static void main(String[] args) {
  39.  
  40. String filePath = "C:\\Users\\Frage\\Desktop\\FragerPT\\ola.txt";
  41. zipFile(filePath);
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement