Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zip;
- import java.io.*;
- import java.nio.file.*;
- import java.util.zip.*;
- /**
- * This Java program demonstrates how to compress a file in ZIP format.
- *
- * @author www.codejava.net
- */
- public class zip {
- private static void zipFile(String filePath) {
- try {
- filePath = "C:\\Users\\Frage\\Desktop\\FragerPT\\JQlimax";
- File file = new File(filePath);
- // String zipFileName = file.getName().concat(".zip");
- String zipFileName="C:\\Users\\Frage\\Desktop\\FragerPT\\cenas1.zip";
- FileOutputStream fos = new FileOutputStream(zipFileName);
- ZipOutputStream zos = new ZipOutputStream(fos);
- zos.putNextEntry(new ZipEntry(file.getName()));
- byte[] bytes = Files.readAllBytes(Paths.get(filePath));
- zos.write(bytes, 0, bytes.length);
- zos.closeEntry();
- zos.close();
- } catch (FileNotFoundException ex) {
- System.err.format("The file %s does not exist", filePath);
- } catch (IOException ex) {
- System.err.println("I/O error: " + ex);
- }
- }
- public static void main(String[] args) {
- String filePath = "C:\\Users\\Frage\\Desktop\\FragerPT\\ola.txt";
- zipFile(filePath);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement