zip file java

Solutions on MaxInterview for zip file java by the best coders in the world

showing results for - "zip file java"
Clint
12 Apr 2019
1import java.io.FileOutputStream;
2import java.io.IOException;
3import java.nio.file.*;
4import java.nio.file.attribute.BasicFileAttributes;
5import java.util.zip.ZipEntry;
6import java.util.zip.ZipOutputStream;
7
8public class ZipCompress {
9    public static void compress(String dirPath) {
10        final Path sourceDir = Paths.get(dirPath);
11        String zipFileName = dirPath.concat(".zip");
12        try {
13            final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
14            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
15                @Override
16                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
17                    try {
18                        Path targetFile = sourceDir.relativize(file);
19                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
20                        byte[] bytes = Files.readAllBytes(file);
21                        outputStream.write(bytes, 0, bytes.length);
22                        outputStream.closeEntry();
23                    } catch (IOException e) {
24                        e.printStackTrace();
25                    }
26                    return FileVisitResult.CONTINUE;
27                }
28            });
29            outputStream.close();
30        } catch (IOException e) {
31            e.printStackTrace();
32        }
33    }
34}
Lukas
11 May 2017
1import java.io.BufferedOutputStream;
2import java.io.File;
3import java.io.FileOutputStream;
4import java.nio.file.Files;
5import java.nio.file.Paths;
6import java.util.ArrayList;
7import java.util.List;
8import java.util.zip.ZipEntry;
9import java.util.zip.ZipOutputStream;
10
11public class zip {
12
13    public static void main(String[] args) {
14    	zipFolder(mapFolder("Test"));
15    	
16        System.out.println("Done");
17    }
18    
19	public static List<String> mapFolder(String path, boolean includeEmptyFolders) {
20    	List<String> map = new ArrayList<String>();
21    	List<String> unmappedDirs = new ArrayList<String>();
22    	File[] items = new File(path).listFiles();
23
24    	if (!path.substring(path.length() - 1).equals("/")) {
25    		path += "/";
26    	}
27    		
28    	if (items != null) {
29	    	for (File item : items) {
30	    		if (item.isFile()) {
31	    				map.add(path+item.getName());
32	    		} else {
33	    			unmappedDirs.add(path+item.getName());
34	    		}
35	    	}
36	    	
37	    	if (!unmappedDirs.isEmpty()) {
38	    		for (String folder : unmappedDirs) {
39	    			List<String> temp = mapFolder(folder, includeEmptyFolders);
40	    			if (!temp.isEmpty()) {
41	    				for (String item : temp)
42	    					map.add(item);
43    				} else if (includeEmptyFolders == true) {
44    					map.add(folder+"/");
45    				}
46	    		}
47	    	}
48    	}
49    	return map;
50    }
51    
52    public static void zipFolder(String zipPath, List<String> items) {
53    	try {
54            FileOutputStream f = new FileOutputStream(zipPath);
55            ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(f));
56            
57            for (String item : items) {
58            	String contents = String.join("\n", Files.readAllLines(Paths.get(item)));
59            	zip.putNextEntry(new ZipEntry(item));
60            	
61            	byte[] data = contents.getBytes();
62            	zip.write(data, 0, data.length);
63            	zip.closeEntry();
64            }
65		    	
66            zip.close();
67            f.close();
68        } catch(Exception e) {
69            System.out.println(e.getMessage());
70        }
71    }
72}
María Camila
15 Oct 2016
1import java.io.BufferedInputStream;
2import java.io.FileInputStream;
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.Date;
7import java.util.Enumeration;
8import java.util.zip.ZipEntry;
9import java.util.zip.ZipFile;
10import java.util.zip.ZipInputStream;
11
12/**
13 * Java program to iterate and read file entries from Zip archive.
14 * This program demonstrate two ways to retrieve files from Zip using ZipFile and by using ZipInputStream class.
15 * @author Javin
16 */
17
18public class ZipFileReader {
19
20    // This Zip file contains 11 PNG images
21    private static final String FILE_NAME = "C:\\temp\\pics.zip";
22    private static final String OUTPUT_DIR = "C:\\temp\\Images\\";
23    private static final int BUFFER_SIZE = 8192;
24
25    public static void main(String args[]) throws IOException {
26
27        // Prefer ZipFile over ZipInputStream
28        readUsingZipFile();
29    //  readUsingZipInputStream();
30
31    }
32
33    /*
34     * Example of reading Zip archive using ZipFile class
35     */
36
37    private static void readUsingZipFile() throws IOException {
38        final ZipFile file = new ZipFile(FILE_NAME);
39        System.out.println("Iterating over zip file : " + FILE_NAME);
40
41        try {
42            final Enumeration<? extends ZipEntry> entries = file.entries();
43            while (entries.hasMoreElements()) {
44                final ZipEntry entry = entries.nextElement();
45                System.out.printf("File: %s Size %d  Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
46                extractEntry(entry, file.getInputStream(entry));
47            }
48            System.out.printf("Zip file %s extracted successfully in %s", FILE_NAME, OUTPUT_DIR);
49        } finally {
50            file.close();
51        }
52
53    }
54
55    /*
56     * Example of reading Zip file using ZipInputStream in Java.
57     */
58
59    private static void readUsingZipInputStream() throws IOException {
60        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
61        final ZipInputStream is = new ZipInputStream(bis);
62
63        try {
64            ZipEntry entry;
65            while ((entry = is.getNextEntry()) != null) {
66                System.out.printf("File: %s Size %d  Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
67                extractEntry(entry, is);
68            }
69        } finally {
70            is.close();
71        }
72
73    }
74
75    /*
76     * Utility method to read  data from InputStream
77     */
78
79    private static void extractEntry(final ZipEntry entry, InputStream is) throws IOException {
80        String exractedFile = OUTPUT_DIR + entry.getName();
81        FileOutputStream fos = null;
82
83        try {
84            fos = new FileOutputStream(exractedFile);
85            final byte[] buf = new byte[BUFFER_SIZE];
86            int read = 0;
87            int length;
88
89            while ((length = is.read(buf, 0, buf.length)) >= 0) {
90                fos.write(buf, 0, length);
91            }
92
93        } catch (IOException ioex) {
94            fos.close();
95        }
96
97    }
98
99}
100
101Output:
102Iterating over zip file : C:\temp\pics.zip
103File: Image  (11).png Size 21294  Modified on 10/24/13
104File: Image  (1).png Size 22296  Modified on 11/19/13
105File: Image  (2).png Size 10458  Modified on 10/24/13
106File: Image  (3).png Size 18425  Modified on 11/19/13
107File: Image  (4).png Size 31888  Modified on 11/19/13
108File: Image  (5).png Size 27454  Modified on 11/19/13
109File: Image  (6).png Size 67608  Modified on 11/19/13
110File: Image  (7).png Size 8659  Modified on 11/19/13
111File: Image  (8).png Size 40015  Modified on 11/19/13
112File: Image  (9).png Size 17062  Modified on 10/24/13
113File: Image  (10).png Size 42467  Modified on 10/24/13
114Zip file C:\temp\pics.zip extracted successfully in C:\temp\Images\
queries leading to this page
read contents of a file from a zip in javaproperly extract a file from zip in javajava zipp unzipjava create zip from filehow to write code for creating zip files in javahow to zip files using javaziping the file in javahow to zip a file using javaread data from zip witj javaread a zip file in java in java and write to textopen zip file in javajava 8 unzip one filegenerate zip file in java text filejava how to export zip filedownload zip file in javajava zip zipcreate zip of java projecthow to create a zip in javajava create zipzip file write and download in javajava 8 zip filecreate a zip file of directory javacreate zip file using java 8java zip file before sendingjava export one file from ziphow to serve zip file with javahow to zip a file in javajava zip functionjava output zipwrite a zip file javazipfile javaextract file from zip and read it using javajava unzip filehow to read the zip file in javazip documents javajava create zip filecreateing zip file java 8how to read files from zip folder in javajava read file from zipunzip javadifferent ways to zip a file in java how to create a zip file javajava unpack zipjava zip filejava install zip create zip file using javaextract zip files javajava create zip file codecode to zip files in javaequivalent of zip in javajava util unzipjava convert folder to zip filehow to zip files in javajava decompress zipjava create zip from directoryjava extract zip filejava read a file inside a zip filehow to zip java filesread a zip file in javajava create and store zip in serverwrite zip file javajava generate zip filezip function in javazip file in javaunzip zipentry javagenerate zip file in javadownload a zip file filename 3d javajava make zip filejava read zip filecreate a zip file and download in javaread zip file in javahow to make zip files in javazipfile zipfile example javahow to extract zip file in javajava zip file downloadcreate a zip file javahow to get zip file in javacreare file zip java examplejava creating zip file programmaticallyjava create zip file java niounzip file javajava unzipjava file in zipextract zip file in javajava get file from ziphow to unzip a file using javacreate zip fie javazip slip java examplemake zip file from folder javajava zip and send filesjava open zip filejava code to create zip from filesjava read zip fileszipfile java exampleread zip file javahow to creat e zip in javaread a zip file and download javahow to make a java file into a zip filejava zip compressjava save zip file systemjava zip file systemjava see zip contentssend zip content file javazip java filesunpack zip javajava ziphow to create a zip file using javajava modify zip fileszip a file in javacreate a zip file and name it in javajava zip exampleread filews from zip file using java java zip file downloadcreate zip file javazip in javadownload via zipinputstreamdecompmressjava zip a file how to read a zip file javazip javamake a zip file in javacreate zip file folder javaextract zip file javahow to read in a zip file javahow to write to a zip file javajava download zip filejava read zip file entryjava create a zip filezip file javaspring booy java ziphow to create zip in javazip file extraction javahow to return zip file in javacreate zip javajava extract zip entryhow to make a zip of files in javamake zip file using javacreate a zip file with javajava zip filesspring boot zip filehow to make zip file in javahow can i compress a file without using zip in javajava read file in ziphow to make zip file javazip compression with writing to file javazip in java 5ccreate zip file in java 8create zip with java util zipzip files javacreate zip in javazip file extract javacreate zip file java with folderszip file java