Java compress to jar

Zipping and Unzipping in Java

announcement - icon

As always, the writeup is super practical and based on a simple application that can work with documents with a mix of encrypted and unencrypted fields.

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll learn how to zip a file into an archive and how to unzip the archive, all using core libraries provided by Java.

These core libraries are part of the java.util.zip package, where we can find all zipping- and unzipping-related utilities.

2. Zip a File

First, let’s look at a simple operation, zipping a single file.

For example, we’ll zip a file named test1.txt into an archive named compressed.zip.

Of course, we’ll first access the file from a disk:

public class ZipFile < public static void main(String[] args) throws IOException < String sourceFile = "test1.txt"; FileOutputStream fos = new FileOutputStream("compressed.zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); File fileToZip = new File(sourceFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while((length = fis.read(bytes)) >= 0) < zipOut.write(bytes, 0, length); >zipOut.close(); fis.close(); fos.close(); > >

3. Zip Multiple Files

Next, let’s see how to zip multiple files into one zip file. We’ll compress test1.txt and test2.txt into multiCompressed.zip:

public class ZipMultipleFiles < public static void main(String[] args) throws IOException < String file1 = "src/main/resources/zipTest/test1.txt"; String file2 = "src/main/resources/zipTest/test2.txt"; final ListsrcFiles = Arrays.asList(file1, file2); final FileOutputStream fos = new FileOutputStream(Paths.get(file1).getParent().toAbsolutePath() + "/compressed.zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); for (String srcFile : srcFiles) < File fileToZip = new File(srcFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while((length = fis.read(bytes)) >= 0) < zipOut.write(bytes, 0, length); >fis.close(); > zipOut.close(); fos.close(); > >

4. Zip a Directory

Now, let’s discuss how to zip an entire directory. Here, we’ll compress the zipTest folder into the dirCompressed.zip file:

public class ZipDirectory < public static void main(String[] args) throws IOException < String sourceFile = "zipTest"; FileOutputStream fos = new FileOutputStream("dirCompressed.zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); File fileToZip = new File(sourceFile); zipFile(fileToZip, fileToZip.getName(), zipOut); zipOut.close(); fos.close(); >private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException < if (fileToZip.isHidden()) < return; >if (fileToZip.isDirectory()) < if (fileName.endsWith("/")) < zipOut.putNextEntry(new ZipEntry(fileName)); zipOut.closeEntry(); >else < zipOut.putNextEntry(new ZipEntry(fileName + "/")); zipOut.closeEntry(); >File[] children = fileToZip.listFiles(); for (File childFile : children) < zipFile(childFile, fileName + "/" + childFile.getName(), zipOut); >return; > FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) < zipOut.write(bytes, 0, length); >fis.close(); > >
  • To zip sub-directories, we iterate through them recursively.
  • Every time we find a directory, we append its name to the descendant’s ZipEntry name to save the hierarchy.
  • We also create a directory entry for every empty directory.

5. Append New Files to Zip File

Next, we’ll add a single file to an existing zip file. For example, let’s add file3.txt into compressed.zip:

String file3 = "src/main/resources/zipTest/file3.txt"; Map env = new HashMap<>(); env.put("create", "true"); Path path = Paths.get(Paths.get(file3).getParent() + "/compressed.zip"); URI uri = URI.create("jar:" + path.toUri()); try (FileSystem fs = FileSystems.newFileSystem(uri, env))

In short, we mounted the zip file using .newFileSystem() provided by the FileSystems class, which has been available since JDK 1.7. Then, we created a newFile3.txt inside the compressed folder and added all the contents from file3.txt.

6. Unzip an Archive

Now, let’s unzip an archive and extract its contents.

For this example, we’ll unzip compressed.zip into a new folder named unzipTest:

public class UnzipFile < public static void main(String[] args) throws IOException < String fileZip = "src/main/resources/unzipTest/compressed.zip"; File destDir = new File("src/main/resources/unzipTest"); byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)); ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) < // . >zis.closeEntry(); zis.close(); > >

Inside the while loop, we’ll iterate through each ZipEntry and first check if it’s a directory. If it is, then we’ll create the directory using the mkdirs() method; otherwise, we’ll continue with creating the file:

while (zipEntry != null) < File newFile = newFile(destDir, zipEntry); if (zipEntry.isDirectory()) < if (!newFile.isDirectory() && !newFile.mkdirs()) < throw new IOException("Failed to create directory " + newFile); >> else < // fix for Windows-created archives File parent = newFile.getParentFile(); if (!parent.isDirectory() && !parent.mkdirs()) < throw new IOException("Failed to create directory " + parent); >// write file content FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) < fos.write(buffer, 0, len); >fos.close(); > zipEntry = zis.getNextEntry(); >

One note here is that on the else branch, we’re also checking if the file’s parent directory exists. This is necessary for archives created on Windows, where the root directories don’t have a corresponding entry in the zip file.

Another critical point is in the newFile() method:

public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException < File destFile = new File(destinationDir, zipEntry.getName()); String destDirPath = destinationDir.getCanonicalPath(); String destFilePath = destFile.getCanonicalPath(); if (!destFilePath.startsWith(destDirPath + File.separator)) < throw new IOException("Entry is outside of the target dir: " + zipEntry.getName()); >return destFile; >

The newFile() method guards against writing files to the file system outside the target folder. This vulnerability is called Zip Slip.

7. Conclusion

In this article, we illustrated how to use Java libraries for zipping and unzipping files.

The implementation of these examples can be found over on GitHub.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Compress and Decompress Java JAR File with Apache Compress

In this tutorial we demonstrate how to compress and decompress Java JAR files using Apache Compress. In general, JAR archives are ZIP files, so the JAR package supports all options provided by the ZIP package.

Project Structure

Let’s start by looking at the project structure.

compress-decompress-java-jar-file-apache-compress-project-structure

Maven Dependencies

We use Apache Maven to manage our project dependencies. Make sure the following dependencies reside on the class-path. We use Apache Commons Compress, make sure the org.apache.commons:commons-compress dependency resides on the class-path.

  4.0.0 com.memorynotfound.io.compression jar 1.0.0-SNAPSHOT IO Compression - $ https://memorynotfound.com jar  org.apache.commons commons-compress 1.14     org.apache.maven.plugins maven-compiler-plugin 3.7.0 1.8 1.8     

Compress and Decompress Java JAR File with Apache Compress

JAR archives are ZIP files, so the JAR package supports all options provided by the ZIP package. To be interoperable JAR archives should always be created using the UTF-8 encoding for file names (which is the default).

  • Compressing files in JAR format: We use the JarArchiveOutputStream to compress files and/or directories into JAR format. We can add entries in the archive using the JarArchiveOutputStream.putArchiveEntry method and pass in a JarArchiveEntry as an argument containing the file and filename respectively. .
  • Decompressing JAR archive: We can decompress the JAR archive using the JarArchiveInputStream class. Next, we loop over the JarArchiveEntry using the JarArchiveEntry.getNextJarEntry() class and copy the content to an FileOutputStream .

Archives created using JarArchiveOutputStream will implicitly add a JarMaker extra field to the very first archive entry of the archive which will make Solaris recognize them as Java archives and allows them to be used as executables.

package com.memorynotfound.resource; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class JAR < private JAR() <>public static void compress(String name, File. files) throws IOException < try (JarArchiveOutputStream out = new JarArchiveOutputStream(new FileOutputStream(name)))< for (File file : files)< addToArchiveCompression(out, file, "."); >> > public static void decompress(String in, File destination) throws IOException < try (JarArchiveInputStream jin = new JarArchiveInputStream(new FileInputStream(in)))< JarArchiveEntry entry; while ((entry = jin.getNextJarEntry()) != null) < if (entry.isDirectory())< continue; >File curfile = new File(destination, entry.getName()); File parent = curfile.getParentFile(); if (!parent.exists()) < if (!parent.mkdirs())< throw new RuntimeException("could not create directory: " + parent.getPath()); >> IOUtils.copy(jin, new FileOutputStream(curfile)); > > > private static void addToArchiveCompression(JarArchiveOutputStream out, File file, String dir) throws IOException < String name = dir + File.separator + file.getName(); if (file.isFile())< JarArchiveEntry entry = new JarArchiveEntry(name); out.putArchiveEntry(entry); entry.setSize(file.length()); IOUtils.copy(new FileInputStream(file), out); out.closeArchiveEntry(); >else if (file.isDirectory()) < File[] children = file.listFiles(); if (children != null)< for (File child : children)< addToArchiveCompression(out, child, name); >> > else < System.out.println(file.getName() + " is not supported"); >> >

Java JAR Example

This program demonstrates the JAR archive compression decompression example.

package com.memorynotfound.resource; import java.io.File; import java.io.IOException; public class JARProgram < private static final String OUTPUT_DIRECTORY = "/tmp"; private static final String TAR_GZIP_SUFFIX = ".jar"; private static final String MULTIPLE_RESOURCES = "/example-multiple-resources"; private static final String RECURSIVE_DIRECTORY = "/example-recursive-directory"; private static final String MULTIPLE_RESOURCES_PATH = OUTPUT_DIRECTORY + MULTIPLE_RESOURCES + TAR_GZIP_SUFFIX; private static final String RECURSIVE_DIRECTORY_PATH = OUTPUT_DIRECTORY + RECURSIVE_DIRECTORY + TAR_GZIP_SUFFIX; public static void main(String. args) throws IOException < // class for resource classloading Class clazz = JARProgram.class; // get multiple resources files to compress File resource1 = new File(clazz.getResource("/in/resource1.txt").getFile()); File resource2 = new File(clazz.getResource("/in/resource2.txt").getFile()); File resource3 = new File(clazz.getResource("/in/resource3.txt").getFile()); // compress multiple resources JAR.compress(MULTIPLE_RESOURCES_PATH, resource1, resource2, resource3); // decompress multiple resources JAR.decompress(MULTIPLE_RESOURCES_PATH, new File(OUTPUT_DIRECTORY + MULTIPLE_RESOURCES)); // get directory file to compress File directory = new File(clazz.getResource("/in/dir").getFile()); // compress recursive directory JAR.compress(RECURSIVE_DIRECTORY_PATH, directory); // decompress recursive directory JAR.decompress(RECURSIVE_DIRECTORY_PATH, new File(OUTPUT_DIRECTORY + RECURSIVE_DIRECTORY)); >>

Generated Files

Here is an example of the generated JAR archive.

compress-decompress-java-jar-file-apache-compress-example-output

References

Источник

Читайте также:  Styled components inline css
Оцените статью