Java create zip file

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompress {
    public static void compress(String dirPath) {
        final Path sourceDir = Paths.get(dirPath);
        String zipFileName = dirPath.concat(".zip");
        try {
            final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                    try {
                        Path targetFile = sourceDir.relativize(file);
                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
                        byte[] bytes = Files.readAllBytes(file);
                        outputStream.write(bytes, 0, bytes.length);
                        outputStream.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0
0

                                    import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class zip {

    public static void main(String[] args) {
    	zipFolder(mapFolder(&quot;Test&quot;));
    	
        System.out.println(&quot;Done&quot;);
    }
    
	public static List&lt;String&gt; mapFolder(String path, boolean includeEmptyFolders) {
    	List&lt;String&gt; map = new ArrayList&lt;String&gt;();
    	List&lt;String&gt; unmappedDirs = new ArrayList&lt;String&gt;();
    	File[] items = new File(path).listFiles();

    	if (!path.substring(path.length() - 1).equals(&quot;/&quot;)) {
    		path += &quot;/&quot;;
    	}
    		
    	if (items != null) {
	    	for (File item : items) {
	    		if (item.isFile()) {
	    				map.add(path+item.getName());
	    		} else {
	    			unmappedDirs.add(path+item.getName());
	    		}
	    	}
	    	
	    	if (!unmappedDirs.isEmpty()) {
	    		for (String folder : unmappedDirs) {
	    			List&lt;String&gt; temp = mapFolder(folder, includeEmptyFolders);
	    			if (!temp.isEmpty()) {
	    				for (String item : temp)
	    					map.add(item);
    				} else if (includeEmptyFolders == true) {
    					map.add(folder+&quot;/&quot;);
    				}
	    		}
	    	}
    	}
    	return map;
    }
    
    public static void zipFolder(String zipPath, List&lt;String&gt; items) {
    	try {
            FileOutputStream f = new FileOutputStream(zipPath);
            ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(f));
            
            for (String item : items) {
            	String contents = String.join(&quot;\n&quot;, Files.readAllLines(Paths.get(item)));
            	zip.putNextEntry(new ZipEntry(item));
            	
            	byte[] data = contents.getBytes();
            	zip.write(data, 0, data.length);
            	zip.closeEntry();
            }
		    	
            zip.close();
            f.close();
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to make zip file in Java how to make zip files in Java zip files java zip function in java how to make a zip of files in java java save zip file system java zip function create zip file folder java zipfile java create zip file java with folders How to zip files using java zip in java\ java zip file system how to write code for creating zip files in java make zip file from folder java create a zip file of directory java java read zip file java create a zip file creaTE zip java how to creat e.zip in java make zip file using java create zip file in java 8 equivalent of zip in java create a zip file java create zip of java project java zip file download read a zip file in java java create zip from directory how to create zip in java java create zip from file how to zip java files code to zip files in java create a zip file with java download a zip file filename= java read zip file in java java zip.zip java zip file download create a zip file and name it in java create a zip file and download in java create zip in java how to zip a file using java java zip example java create zip file code create zip file using java 8 create zip file using java zip file write and download in java how to create a zip file using java zipfile java example how to read a zip file java how to create a zip file java zip file java java generate zip file createing zip file java 8 java creating zip file programmatically how to get zip file in java download zip file in java ziping the file in java how can i COMPRESS A FILE WITHOUT USING ZIP in java how to unzip a file using java unzip zipentry java zip in java different ways to zip a file in java java unpack zip unpack zip java java decompress zip how to write to a zip file java java output.zip java install zip java zip files unzip file java java util unzip java see zip contents java modify zip files java make zip file java create and store zip in server unzip java java unzip write zip file java zipfile zipfile example java java extract zip entry zip slip java example zip java files generate zip file in java make a zip file in java spring booy java zip java 8 unzip one file java export one file from zip java zipp unzip download via zipinputstreamdecompmress java zip java create zip file java.nio java open zip file java convert folder to zip file zip documents java zip java Java code to create zip from files how to make zip file java how to zip a file in java spring boot zip file java 8 zip file java unzip file java zip and send files zip compression with writing to file java java zip a file write a zip file java java create zip zip a file in java Create zip file java java zip compress java zip file before sending generate zip file in java text file send zip content file java zip file extraction java zip file in java java zip file java extract zip file java create zip file
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source