Skip to content
/ simplezip Public

Java processing of Zip files that gives full control over all Zip disk structures

License

Notifications You must be signed in to change notification settings

j256/simplezip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Java Zip

Maven Central javadoc ChangeLog CodeCov CircleCI GitHub License

This package provides Java classes to read and write Zip files. There are a number of different libraries that do this (including one built into the JDK) but I've not found any that gave me precise controls over the Zip internal, persisted data structures. This library allows you to control the output of all Zip data and should allow you to read and write Zip files with full precision.

Enjoy. Gray Watson

Getting Started

Reading a Zip File

The following code runs through all of the Zip-file parts. input could be a file path, File, or an InputStream.

ZipFileInput zipInput = new ZipFileInput(input);
// readFileHeader() will return null when no more files to read
ZipFileHeader header = zipInput.readFileHeader();
byte[] buffer = new byte[4096];
// read into buffers or via InputStream until it returns -1
long numRead = zipInput.readFileDataPart(buffer);
...
// can also call readFileData(File) to write out a file from input
// NOTE: descriptor can be null if none in the zip
ZipDataDescriptor dataDescriptor = zipInput.getCurrentDataDescriptor();
// repeat reading file headers and data until readFileHeader() returns null
// read in the optional central-directory file-headers, null when no more
ZipCentralDirectoryFileEntry dirEntry = zipInput.readDirectoryFileEntry();
// read in the optional central-directory end data
ZipCentralDirectoryEnd end = zipInput.readDirectoryEnd();
zipInput.close();

Writing a Zip File

The following code writes out a Zip-file. output could be a file path, File, or OutputStream.

ZipFileOutput zipOutput = new ZipFileOutput(output);
ZipFileHeader header = ZipFileHeader.builder()
	.withFileName("hello.txt")
	.withGeneralPurposeFlags(GeneralPurposeFlag.DEFLATING_MAXIMUM)
	.build();
// write a file-header to the zip-file
zipOutput.writeFileHeader(header);
// add optional central-directory info to the file such as text flag
// this will be written to disk at the end of the zip
zipOutput.addDirectoryFileInfo(
	ZipCentralDirectoryFileInfo.builder().withTextFile(true).build());
// write file data from file, buffer, or InputStream
zipOutput.writeFileDataPart(fileBytes);
...
// must be called after all file parts written
zipOutput.finishFileData();
// can write more file-headers and data here
...
// this writes the recorded central-directory entries, end, and closes tbe zip
zipOutput.close();

Maven Configuration

Maven packages are published via Maven Central

<dependency>
	<groupId>com.j256.simplezip</groupId>
	<artifactId>simplezip</artifactId>
	<version>2.2</version>
</dependency>

Dependencies

SimpleZip has no runtime dependencies.

ChangeLog Release Notes

See the ChangeLog

References Used to Write this Library

Here are some of the reference documents that were used to write this library.

About

Java processing of Zip files that gives full control over all Zip disk structures

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages