Java tar to zip

Tech Tutorials

Tutorials and posts about Java, Spring, Hadoop and many more. Java code examples and interview questions. Spring code examples.

Friday, May 1, 2020

Creating Tar File And GZipping Multiple Files in Java

If you want to GZIP multiple files that can’t be done directly as you can only compress a single file using GZIP. In order to GZIP multiple files you will have to archive multiple files into a tar and then compress it to create a .tar.gz compressed file. In this post we’ll see how to create a tar file in Java and gzip multiple files.

Using Apache Commons Compress

Here I am posting a Java program to create a tar file using Apache Commons Compress library. You can download it from here– https://commons.apache.org/proper/commons-compress/download_compress.cgi

Make sure to add commons-compress-xxx.jar in your application’s class path. I have used commons-compress-1.13 version.

Steps to create tar files

  1. Create a FileOutputStream to the output file (.tar.gz) file.
  2. Create a GZIPOutputStream which will wrap the FileOutputStream object.
  3. Create a TarArchiveOutputStream which will wrap the GZIPOutputStream object.
  4. Then you need to read all the files in a folder.
  5. If it is a directory then just add it to the TarArchiveEntry.
  6. If it is a file then add it to the TarArchiveEntry and also write the content of the file to the TarArchiveOutputStream.

Folder Structure used

Here is a folder structure used in this post to read the files. Test, Test1 and Test2 are directories here and then you have files with in those directories. Your Java code should walk through the whole folder structure and create a tar file with all the entries for the directories and files and then compress it.

Test abc.txt Test1 test.txt test1.txt Test2 xyz.txt

Creating tar file in Java example

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; public class TarGZIPDemo < public static void main(String[] args) < String SOURCE_FOLDER = "/home/netjs/Documents/netjs/Test"; TarGZIPDemo tGzipDemo = new TarGZIPDemo(); tGzipDemo.createTarFile(SOURCE_FOLDER); >private void createTarFile(String sourceDir) < TarArchiveOutputStream tarOs = null; try < File source = new File(sourceDir); // Using input name to create output name FileOutputStream fos = new FileOutputStream(source.getAbsolutePath().concat(".tar.gz")); GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos)); tarOs = new TarArchiveOutputStream(gos); addFilesToTarGZ(sourceDir, "", tarOs); >catch (IOException e) < // TODO Auto-generated catch block e.printStackTrace(); >finally < try < tarOs.close(); >catch (IOException e) < // TODO Auto-generated catch block e.printStackTrace(); >> > public void addFilesToTarGZ(String filePath, String parent, TarArchiveOutputStream tarArchive) throws IOException < File file = new File(filePath); // Create entry name relative to parent file path String entryName = parent + file.getName(); // add tar ArchiveEntry tarArchive.putArchiveEntry(new TarArchiveEntry(file, entryName)); if(file.isFile())< FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); // Write file content to archive IOUtils.copy(bis, tarArchive); tarArchive.closeArchiveEntry(); bis.close(); >else if(file.isDirectory()) < // no need to copy any content since it is // a directory, just close the outputstream tarArchive.closeArchiveEntry(); // for files in the directories for(File f : file.listFiles())< // recursively call the method for all the subdirectories addFilesToTarGZ(f.getAbsolutePath(), entryName+File.separator, tarArchive); >> > >

On opening the created .tar.gz compressed file using archive manager.

Читайте также:  Common exceptions in python

That’s all for this topic Creating Tar File And GZipping Multiple Files in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Источник

How to create .zip or .tar Programmatically in Java using Apache Commons Archivers and Compressors

How to create .zip and .tar in Java programatically

Apache foundation by default comes with lots and lots of utilities for us to use. In most of the cases we are kind of unaware of the utility which exists for use to use in our production environment.

In this tutorial we will go over one of the utility by which we could compress any file or directory programmatically in Java. In other words simple archives utility.

Why we need this utility?

Sometime back I’ve written an article on how to upload files using Spring MVC architecture. If you have very big file and you are hosting other users file in some of the file system like netapp or filer or etc then you may want to compress files before upload. You could marry below code into your application to achieve same purpose.

Let’s checkout a result first to better understand:

Before:

Crunchify Tar Utility - File and Directory .zip or .tar

After:

crunchify*.zip file created by Java program

Let’s get started:

  • Create class CrunchifyCompressArchivesUtility.java
  • Add below maven dependency to your project.
  • If you don’t have maven project then follow these steps.
 org.apache.commons commons-compress 1.9  
  • We are going to use Apache Commons compress archivers utility
  • TarArchiveEntry class represents an entry in a Tar archive. It consists of the entry’s header, as well as the entry’s File. Entries can be instantiated in one of three ways, depending on how they are to be used.
  • TarArchiveOutputStream class writes a UNIX tar archive as an OutputStream.
  • We will archive just a file first
  • Also in the same program we will archive a directory
  • We are going to convert file and directory to .zip file. If you want .tar then just change code below.
  • Please change path in below code
package com.crunchify.tutorials; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; /** * @author Crunchify.com * */ public class CrunchifyCompressArchivesUtility < private static final String CRUNCHIFY_BASEDIR = ""; // Default output path private static final String CRUNCHIFY_PATH = "/Users//Desktop/"; // .zip or .tar as per need private static final String FILE_EXTENSION = ".zip"; public static void main(String[] args) < try < // Archive File crunchfyArchive("/Users/appshah/Desktop/crunchifyTarFile.txt"); log("Archive a file task completed. \n"); // Archive Directory crunchfyArchive("/Users/appshah/Desktop/crunchifyTarDirectory"); log("Archive a Directory task completed. "); >catch (Exception e) < log(e.getStackTrace().toString()); >> public static void crunchfyArchive(String srcPath) throws Exception < File crunchifySourceFile = new File(srcPath); // Returns the name of the file or directory denoted by this abstract pathname String crunchifyFileName = crunchifySourceFile.getName(); // Returns the pathname string of this abstract pathname's parent String crunchifyBaseFileNamePath = crunchifySourceFile.getParent(); String destPath = crunchifyBaseFileNamePath + File.separator + crunchifyFileName + FILE_EXTENSION; log("Archived Location: " + destPath); TarArchiveOutputStream outputStream = new TarArchiveOutputStream( new FileOutputStream(new File(destPath))); crunchfyArchive(crunchifySourceFile, outputStream, CRUNCHIFY_BASEDIR); // Flushes this output stream and forces any buffered output bytes to be written out outputStream.flush(); // Closes the underlying OutputStream outputStream.close(); >private static void crunchfyArchive(File crunchifySourceFile, TarArchiveOutputStream outputStream, String crunchifyBasePath) throws Exception < if (crunchifySourceFile.isDirectory()) < // Archive Directory archiveCrunchifyDirectory(crunchifySourceFile, outputStream, crunchifyBasePath); >else < // Archive File archiveCrunchifyFile(crunchifySourceFile, outputStream, crunchifyBasePath); >> private static void archiveCrunchifyDirectory(File crunchifyDirectory, TarArchiveOutputStream outputStream, String crunchifyBasePath) throws Exception < // Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname File[] crunchifyFiles = crunchifyDirectory.listFiles(); if (crunchifyFiles != null) < if (crunchifyFiles.length < 1) < // Construct an entry with only a name. This allows the programmer to construct the entry's header "by hand". File // is set to null TarArchiveEntry entry = new TarArchiveEntry( crunchifyBasePath + crunchifyDirectory.getName() + CRUNCHIFY_PATH); // Put an entry on the output stream outputStream.putArchiveEntry(entry); // Close an entry. This method MUST be called for all file entries that contain data outputStream.closeArchiveEntry(); >// Repeat for all files for (File crunchifyFile : crunchifyFiles) < crunchfyArchive(crunchifyFile, outputStream, crunchifyBasePath + crunchifyDirectory.getName() + CRUNCHIFY_PATH); >> > private static void archiveCrunchifyFile(File crunchifyFile, TarArchiveOutputStream outputStream, String crunchifyDirectory) throws Exception < TarArchiveEntry crunchifyEntry = new TarArchiveEntry( crunchifyDirectory + crunchifyFile.getName()); // Set this entry's file size crunchifyEntry.setSize(crunchifyFile.length()); outputStream.putArchiveEntry(crunchifyEntry); BufferedInputStream inputStream = new BufferedInputStream( new FileInputStream(crunchifyFile)); int counter; // 512: buffer size byte byteData[] = new byte[512]; while ((counter = inputStream.read(byteData, 0, 512)) != -1) < outputStream.write(byteData, 0, counter); >inputStream.close(); outputStream.closeArchiveEntry(); > // Crunchify's favorite log utility private static void log(String string) < System.out.println(string); >>
Archived Location: /Users//Desktop/crunchifyTarFile.txt.zip Archive a file task completed. Archived Location: /Users//Desktop/crunchifyTarDirectory.zip Archive a Directory task completed.

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion. 👋

Читайте также:  Convert string into int in python

Suggested Articles.

Источник

Java TAR example – compress and decompress *.tar or *.tar.gz files

This tutorial demonstrate how to compress files or directories recursively in .tar or .tar.gz format and how to decompress a .tar or .tar.gz file.

Project Structure

Let’s start by looking at the project structure.

java-tar-example-compress-and-decompress-tar-gzip-files-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 tar 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 *.tar files

  • Compressing files in .tar format (also known as tarring): We use the TarArchiveOutputStream to compress files and/or directories into TAR format. We can add entries in the archive using the TarArchiveOutputStream.putArchiveEntry mehtod and pass in a TarArchiveEntry as an argument containing the file and filename respectively. .
  • Decompressing .tar archive (also known as untarring): We can untar the TAR archive using the TarArchiveInputStream class. Next, we loop over the TarArchiveEntry using the TarArchiveEntry.getNextTarEntry() class and copy the content to an FileOutputStream .
package com.memorynotfound.resource; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; 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 TAR < private TAR() < >public static void compress(String name, File. files) throws IOException < try (TarArchiveOutputStream out = getTarArchiveOutputStream(name))< for (File file : files)< addToArchiveCompression(out, file, "."); >> > public static void decompress(String in, File out) throws IOException < try (TarArchiveInputStream fin = new TarArchiveInputStream(new FileInputStream(in)))< TarArchiveEntry entry; while ((entry = fin.getNextTarEntry()) != null) < if (entry.isDirectory()) < continue; >File curfile = new File(out, entry.getName()); File parent = curfile.getParentFile(); if (!parent.exists()) < parent.mkdirs(); >IOUtils.copy(fin, new FileOutputStream(curfile)); > > > private static TarArchiveOutputStream getTarArchiveOutputStream(String name) throws IOException < TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(name)); // TAR has an 8 gig file limit by default, this gets around that taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // TAR originally didn't support long file names, so enable the support for it taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); taos.setAddPaxHeadersForNonAsciiNames(true); return taos; >private static void addToArchiveCompression(TarArchiveOutputStream out, File file, String dir) throws IOException < String entry = dir + File.separator + file.getName(); if (file.isFile())< out.putArchiveEntry(new TarArchiveEntry(file, entry)); try (FileInputStream in = new FileInputStream(file))< IOUtils.copy(in, out); >out.closeArchiveEntry(); > else if (file.isDirectory()) < File[] children = file.listFiles(); if (children != null)< for (File child : children)< addToArchiveCompression(out, child, entry); >> > else < System.out.println(file.getName() + " is not supported"); >> >

Compress and Decompress *.tar.gz files

We can also compress or decompress TAR archives in GZIP to save some space. To compress files or directories into .tar.gz format wrap the GzipCompressorOutputStream inside the TarArchiveOutputStream . To decompress .tar.gz archive wrap the GzipCompressorInputStream inside the TarArchiveInputStream .

// compressing *.tar.gz format TarArchiveOutputStream taos = new TarArchiveOutputStream(new GzipCompressorOutputStream(new FileOutputStream(name))); // decompressing *.tar.gz files TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(in))) 

Java Tar and Untar Example

This program demonstrates the tar archive compression decompression example.

package com.memorynotfound.resource; import java.io.File; import java.io.IOException; public class TARProgram < private static final String OUTPUT_DIRECTORY = "/tmp"; private static final String JAR_SUFFIX = ".tar"; 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 + JAR_SUFFIX; private static final String RECURSIVE_DIRECTORY_PATH = OUTPUT_DIRECTORY + RECURSIVE_DIRECTORY + JAR_SUFFIX; public static void main(String. args) throws IOException < // class for resource classloading Class clazz = TARProgram.class; // get multiple resources files to compress File resource1 = new File(clazz.getResource("/resource1.txt").getFile()); File resource2 = new File(clazz.getResource("/resource2.txt").getFile()); File resource3 = new File(clazz.getResource("/resource3.txt").getFile()); // compress multiple resources TAR.compress(MULTIPLE_RESOURCES_PATH, resource1, resource2, resource3); // decompress multiple resources TAR.decompress(MULTIPLE_RESOURCES_PATH, new File(OUTPUT_DIRECTORY + MULTIPLE_RESOURCES)); // get directory file to compress File directory = new File(clazz.getResource("/dir").getFile()); // compress recursive directory TAR.compress(RECURSIVE_DIRECTORY_PATH, directory); // decompress recursive directory TAR.decompress(RECURSIVE_DIRECTORY_PATH, new File(OUTPUT_DIRECTORY + RECURSIVE_DIRECTORY)); >>

Generated Files

Here is an example of the generated tar archive.

Читайте также:  Python dictionary delete all keys

java-tar-example-compress-and-decompress-tar-gzip-files-example-output

References

Источник

Оцените статью