Java apache common zip

Extract .zip File in Java using Apache Commons Compress

In this Java tutorial, we learn how to use the Apache Commons Compress library to extract a .zip file into a directory in the Java program.

Add Apache Commons Compress library to your Java project

To use Apache Commons Compress Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'org.apache.commons', name: 'commons-compress', version: '1.20'

To use Apache Commons Compress Java library in the Maven build project, add the following dependency into the pom.xml file.

  org.apache.commons commons-compress 1.20 

To download the Apache Commons Compress jar file you can visit Apache Commons Compress download page at commons.apache.org

Implement ZipFileCompressUtils class

First step, we implement a new class named ZipFileCompressUtils and introduce extractZip() public method to extract a .zip file into a directory.

import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.utils.IOUtils; import java.io.File; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class ZipFileCompressUtils  public void extractZip(String zipFilePath, String extractDirectory)  InputStream inputStream = null; try  Path filePath = Paths.get(zipFilePath); inputStream = Files.newInputStream(filePath); ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory(); ArchiveInputStream archiveInputStream = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.ZIP, inputStream); ArchiveEntry archiveEntry = null; while((archiveEntry = archiveInputStream.getNextEntry()) != null)  Path path = Paths.get(extractDirectory, archiveEntry.getName()); File file = path.toFile(); if(archiveEntry.isDirectory())  if(!file.isDirectory())  file.mkdirs(); > > else  File parent = file.getParentFile(); if(!parent.isDirectory())  parent.mkdirs(); > try (OutputStream outputStream = Files.newOutputStream(path))  IOUtils.copy(archiveInputStream, outputStream); > > > > catch (IOException e)  e.printStackTrace(); > catch (ArchiveException e)  e.printStackTrace(); > > >

Extract .zip File into a Directory

In the following example Java program, we use the ZipFileCompressUtils class implemented above to extract a .zip file into a directory. For example we have the following data in this program.

  • “D:\SimpleSolution\sample.zip” is a zip file that needs to be extracted.
  • “D:\SimpleSolution\output” is the out directory to write extracted files.
public class ExtractZipFileExample  public static void main(String[] args)  String zipPath = "D:\\SimpleSolution\\sample.zip"; String outputDirectory = "D:\\SimpleSolution\\output"; ZipFileCompressUtils zipFileCompressUtils = new ZipFileCompressUtils(); zipFileCompressUtils.extractZip(zipPath, outputDirectory); > >

Источник

Create .zip File in Java using Apache Commons Compress

In this Java tutorial, we learn how to use the Apache Commons Compress library to create a .zip file in the Java program.

Add Apache Commons Compress library to your Java project

To use Apache Commons Compress Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'org.apache.commons', name: 'commons-compress', version: '1.20'

To use Apache Commons Compress Java library in the Maven build project, add the following dependency into the pom.xml file.

  org.apache.commons commons-compress 1.20 

To download the Apache Commons Compress jar file you can visit Apache Commons Compress download page at commons.apache.org

Implement ZipFileCompressUtils class

First step, we implement a new class named ZipFileCompressUtils and introduce createZipFile() public method to create a .zip file from a file or directory source.

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class ZipFileCompressUtils  public void createZipFile(String zipFileName, String fileOrDirectoryToZip)  BufferedOutputStream bufferedOutputStream = null; ZipArchiveOutputStream zipArchiveOutputStream = null; OutputStream outputStream = null; try  Path zipFilePath = Paths.get(zipFileName); outputStream = Files.newOutputStream(zipFilePath); bufferedOutputStream = new BufferedOutputStream(outputStream); zipArchiveOutputStream = new ZipArchiveOutputStream(bufferedOutputStream); File fileToZip = new File(fileOrDirectoryToZip); addFileToZipStream(zipArchiveOutputStream, fileToZip, ""); zipArchiveOutputStream.close(); bufferedOutputStream.close(); outputStream.close(); > catch (IOException e)  e.printStackTrace(); > > private void addFileToZipStream(ZipArchiveOutputStream zipArchiveOutputStream, File fileToZip, String base) throws IOException  String entryName = base + fileToZip.getName(); ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(fileToZip, entryName); zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry); if(fileToZip.isFile())  FileInputStream fileInputStream = null; try  fileInputStream = new FileInputStream(fileToZip); IOUtils.copy(fileInputStream, zipArchiveOutputStream); zipArchiveOutputStream.closeArchiveEntry(); > finally  IOUtils.closeQuietly(fileInputStream); > > else  zipArchiveOutputStream.closeArchiveEntry(); File[] files = fileToZip.listFiles(); if(files != null)  for (File file: files)  addFileToZipStream(zipArchiveOutputStream, file, entryName + "/"); > > > > >

Create New .zip File from a Directory

In the following example Java program, we use the above class to create a new file file. In the example we have

  • “D:\SimpleSolution\sample.zip” is the .zip file that is expected to be created.
  • “D:\SimpleSolution\sample\” is the directory that needs to be zip.
public class CreateZipFileFromDirectoryExample  public static void main(String[] args)  String zipPath = "D:\\SimpleSolution\\sample.zip"; String directoryToZip = "D:\\SimpleSolution\\sample\\"; ZipFileCompressUtils zipFileCompressUtils = new ZipFileCompressUtils(); zipFileCompressUtils.createZipFile(zipPath, directoryToZip); > >

Create New .zip File from a File

In the following example Java program, we create .zip file from a source file for example “D:\SimpleSolution\sample\test.txt”

public class CreateZipFileFromFileExample  public static void main(String[] args)  String zipPath = "D:\\SimpleSolution\\sample.zip"; String fileToZip = "D:\\SimpleSolution\\sample\\test.txt"; ZipFileCompressUtils zipFileCompressUtils = new ZipFileCompressUtils(); zipFileCompressUtils.createZipFile(zipPath, fileToZip); > >

Источник

Package org.apache.commons.compress.archivers.zip

An interface added to allow access to the character set associated with an NioZipEncoding , without requiring a new method to be added to ZipEncoding .

Handles extra field data that doesn’t follow the recommended pattern for extra fields with a two-byte key and a two-byte length.

Parser/encoder for the «general purpose bit» field in ZIP’s local file and central directory headers.

If this extra field is added as the very first extra field of the archive, Solaris will consider it an executable jar file.

An extra field who’s sole purpose is to align and pad the local file header so that the entry’s data starts at a certain position.

Wrapper for extra field data that doesn’t conform to the recommended format of header-tag + size + data.

Extension that adds better handling of extra fields and provides access to the internal and external file attributes.

Reimplementation of java.util.zip.ZipOutputStream to handle the extended functionality of this package, especially internal/external file attributes and extra fields with different layouts for local file data and central directory entries.

Utility class that represents an eight byte integer with conversion rules for the little endian byte order of ZIP files.

Utility class that represents a four byte integer with conversion rules for the little endian byte order of ZIP files.

Utility class that represents a two byte integer with conversion rules for the little endian byte order of ZIP files.

List of known compression methods Many of these methods are currently not supported by commons compress

Exception thrown when attempting to read or write data for a ZIP entry that uses ZIP features not supported by this library.

Exception thrown when attempting to write data that requires Zip64 support to an archive and UseZip64 has been set to Never .

Источник

Читайте также:  Php di container example
Оцените статью