java create zip file

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

showing results for - "java create zip file"
Bronwyn
27 Jan 2017
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}
Édouard
29 Aug 2018
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}
queries leading to this page
create zip of java projectjava zipp unzipcreare file zip java exampleunzip javajava util unzipjava read zip filedownload a zip file filename 3d javazip file write and download in javacreate zip file java with folderscreate zip file javahow to unzip a file using javahow to make a java file into a zip filecreate zip file in java 8 java zip file downloadzip javacreate zip file using java 8java zip filescreate a zip file with javahow to create zip in javajava ziphow can i compress a file without using zip in javajava save zip file systemunpack zip javazip in javahow to serve zip file with javajava zip and send filesread a zip file in javajava create zip from filecreate zip fie javajava install zip zip function in javahow to zip a file in javaequivalent of zip in javajava creating zip file programmaticallyjava see zip contentsziping the file in javawrite zip file javaunzip zipentry javahow to get zip file in javajava zip compresszip file extraction javahow to make zip files in javahow to make a zip of files in javahow to zip files using javahow to create a zip file using javajava create zip from directoryhow to zip java filesjava 8 unzip one filezipfile zipfile example javadownload zip file in javazip file javajava zip file systemjava 8 zip filejava file in zipjava create zip file codejava create a zip filespring boot zip filezip slip java examplehow to write code for creating zip files in javahow to write to a zip file javaread a zip file and download javacreate a zip file and download in javahow to create a zip file javajava unzip filemake zip file using javajava create and store zip in servermake a zip file in javajava zip filehow to creat e zip in javahow to zip the files in javajava decompress zipzip files javajava unziphow to zip files in javaunzip file javacode to zip files in javagenerate zip file in java text filezip java filesjava modify zip fileszip documents javazipfile java examplecreate zip file using javajava unpack zipjava zip file downloadjava download zip filejava code to create zip from fileswrite a zip file javajava open zip filehow to make zip file in javazip file in javajava zip functionjava zip a file spring booy java zipgenerate zip file in javajava read zip filesjava how to export zip filejava create zip filejava make zip filecreate a zip file javadownload via zipinputstreamdecompmresshow to make zip file javahow to read a zip file javajava extract zip filejava export one file from zipjava create zipzip a file in javazip compression with writing to file javajava output zipjava create zip file java niocreateing zip file java 8java zip zipcreate a zip file and name it in javahow to create a zip in javacreate zip with java util zipzip in java 5cread zip file in javahow to zip a file using javaopen zip file in javamake zip file from folder javacreate zip file folder javajava zip file before sendingjava generate zip filecreate zip in javajava extract zip entryjava zip examplejava convert folder to zip filecreate zip javazipfile javasend zip content file javadifferent ways to zip a file in java create a zip file of directory javajava create zip file