SimpleZip - Simple Zip Java Package

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.

Reading a Zip File

The following code runs through all of the Zip-file parts. input could be a File, file path, 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, file path, 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();

More 256 Sources

Free Spam Protection   Android ORM   Simple Java Zip   JMX using HTTP   Great Eggnog Recipe