import java.util.zip.*;
import java.io.*;CheckedOutputStream cos = new CheckedOutputStream(
new FileOutputStream("C:/123.zip"),new Adler32());
ZipOutputStream zout = new ZipOutputStream(cos);
zout.putNextEntry(new ZipEntry("SRC"));
zout.write("哈哈".getBytes());
zout.close();
import java.io.*;CheckedOutputStream cos = new CheckedOutputStream(
new FileOutputStream("C:/123.zip"),new Adler32());
ZipOutputStream zout = new ZipOutputStream(cos);
zout.putNextEntry(new ZipEntry("SRC"));
zout.write("哈哈".getBytes());
zout.close();
/*
** a simple ZIP tool
**
** ex. java Zip file.1 file.2 > file.zip
**
*/
import java.io.*;
import java.util.zip.*;class Zip {
public static void main(String args[]) throws IOException {
byte b[] = new byte[512];
ZipOutputStream zout = new ZipOutputStream(System.out);
for(int i = 0; i < args.length; i ++) {
InputStream in = new FileInputStream(args[i]);
ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));
zout.putNextEntry(e);
int len=0;
while((len=in.read(b)) != -1) {
zout.write(b,0,len);
}
zout.closeEntry();
print(e);
}
zout.close();
}
public static void print(ZipEntry e){
PrintStream err = System.err;
err.print("added " + e.getName());
if (e.getMethod() == ZipEntry.DEFLATED) {
long size = e.getSize();
if (size > 0) {
long csize = e.getCompressedSize();
long ratio = ((size-csize)*100) / size;
err.println(" (deflated " + ratio + "%)");
}
else {
err.println(" (deflated 0%)");
}
}
else {
err.println(" (stored 0%)");
}
}
}
=================================
Expand the compressed (ZIP) file
/*
** a simple unZIP tool
**
** ex. java UnZip file.zip file1 to unzip file 1 from file.zip
** java UnZip file.zip to unzip file.zip
**
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;class UnZip {
public static void main(String args[]) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry e; while((e=zin.getNextEntry())!= null) {
if (args.length > 1) {
if (e.getName().equals(args[1])) {
unzip(zin, args[1]);
break;
}
}
unzip(zin, e.getName());
}
zin.close();
}
public static void unzip(ZipInputStream zin, String s) throws IOException {
System.out.println("unzipping " + s);
FileOutputStream out = new FileOutputStream(s);
byte [] b = new byte[512];
int len = 0;
while ( (len=zin.read(b))!= -1 ) {
out.write(b,0,len);
}
out.close();
}
}
==========================
Display compressed (ZIP) file content
/*
** a simple viewZIP tool
**
** ex. java ViewZip file.zip
**
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;class ViewZip {
public static void main(String args[]) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry e;
System.err.println("Size\t Date Time Method Ratio Name");
System.err.println("----\t ---- ---- ------ ----- ----");
while((e=zin.getNextEntry())!= null) {
zin.closeEntry();
print(e);
}
zin.close();
}
public static void print(ZipEntry e) {
PrintStream err = System.err;
err.print(e.getSize() + "\t");
DateFormat df = new SimpleDateFormat ("yyyy.mm.dd hh:mm:ss");
Date d = new Date(e.getTime());
err.print(df.format(d) + " ");
if (e.getMethod() == ZipEntry.DEFLATED) {
err.print("deflated ");
long size = e.getSize();
if (size > 0) {
long csize = e.getCompressedSize();
long ratio = ((size-csize)*100) / size;
if (ratio < 10) {
err.write(' ');
}
err.print(ratio + "% ");
}
else {
err.print(" 0% ");
}
}
else {
err.println(" (stored 0 %");
} err.println(e.getName());
}
}
import java.io.*;
import java.util.Vector;
import java.util.zip.*;
/**
* JZip can zip and unzip files or directories compressed or not.
* It can be used as application or as API.
*
* @author Sean Jiang(JiangShangjia) China Technical Development Center
* Sun Microsystems, Inc.
*/
public class JZip
{
protected String outputName;
protected Vector inputNames = null;
protected int compressMethod = ZipEntry.DEFLATED;
/**
* Constructor
* @param outputName output zip file name or unzip output path
* @param inNames input files or directories names to zip or unzip
*/
public JZip(String outputName, Vector inNames)
{
this.outputName = outputName;
this.inputNames = inNames;
}
/**
* Constructor
*/
public JZip()
{ }// Test method
public String test()
{
return "test ok!";
}public void Init(String outputName, Vector inNames)
{
this.outputName = outputName;
this.inputNames = inNames;
}/**
* Set zip compress method type
* @param method set method as ZipEntry.STORED to uncompress or
* ZipEntry.DEFLATED to compress
*/
public void setMethod(int method)
{
if (method == ZipEntry.STORED || method == ZipEntry.DEFLATED)
{
compressMethod = method;
}
}
/**
* Zip uncompressed
* @exception IOException if I/O error occurs
*/
public void zipUncompressed() throws IOException
{
compressMethod = ZipEntry.STORED;
zip();
}
/**
* Do zipping by the instance which has been created by the constructor
* JZip(String, Vector).
* @exception IOException if I/O error occurs
*/
public void zip() throws IOException
{
ByteArrayOutputStream byteos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(byteos);
zip(bos);
bos.close();FileOutputStream fout = new FileOutputStream(outputName);
byte[] bytes = byteos.toByteArray();
fout.write(bytes);
byteos.close();
fout.close();
}
/**
* Do zipping by special output stream and input files or directories.
* @exception IOException if I/O error occurs
*/
public void zip(OutputStream os, Vector inputNames) throws IOException
{
this.inputNames = inputNames;
zip(os);
}
private void zip(OutputStream os) throws IOException
{
ZipOutputStream zout = new ZipOutputStream(os);
File file = null;
String inputName = null;
for (int i = 0; i < inputNames.size(); ++i)
{
inputName = (String)inputNames.elementAt(i);
file = new File(inputName);
if (file.isFile())
{
zipFile(zout, inputName);
}
else if (file.isDirectory())
{
if (!inputName.endsWith(File.separator))
{
inputName = inputName + File.separator;
}
ZipEntry zent = new ZipEntry(inputName);
zent.setMethod(compressMethod);
if (compressMethod == ZipEntry.STORED)
{
byte[] buff = new byte[inputName.length()];
System.arraycopy(inputName, 0, buff, 0, inputName.length());
CRC32 crc = new CRC32();
crc.update(buff);
zent.setSize(inputName.length());
zent.setCrc(crc.getValue());
}
zout.putNextEntry(zent);
zout.closeEntry();
zipDirectory(zout, inputName, file);
}
}
zout.close();
}
private void zipDirectory(ZipOutputStream zos, String directoryPath, File directoryFile) throws IOException
{
if (directoryFile.canRead())
{
String[] fileNames = directoryFile.list();
File file = null;if (!directoryPath.endsWith(File.separator))
{
directoryPath = directoryPath + File.separator;
}for (int i = 0; i < fileNames.length; i++)
{
fileNames = directoryPath + fileNames;
file = new File(fileNames);
if (file.isDirectory())
{
if (!fileNames.endsWith(File.separator))
{
fileNames = fileNames + File.separator;
}
ZipEntry zent = new ZipEntry(fileNames);
zent.setMethod(compressMethod);
if (compressMethod == ZipEntry.STORED)
{
byte[] buff = new byte[fileNames.length()];
System.arraycopy(fileNames, 0, buff, 0, fileNames.length());
CRC32 crc = new CRC32();
crc.update(buff);
zent.setSize(fileNames.length());
zent.setCrc(crc.getValue());
}
zos.putNextEntry(zent);
zos.closeEntry();
zipDirectory(zos, fileNames, file);
}
else if (file.canRead())
{
zipFile(zos, fileNames);
}
else
{
throw new IOException("The File '" + fileNames + "' can't be read.");
}
}
}
else
{
throw new IOException("The directory '" + directoryPath + "' can't be read.");
}
}
private void zipFile(ZipOutputStream zos, String inputFileName) throws IOException
{
byte[] buffer = new byte[1024];
int readCount = 0;
FileInputStream fin = new FileInputStream(inputFileName);
ZipEntry zent = new ZipEntry(inputFileName);
zent.setMethod(compressMethod);
if (compressMethod == ZipEntry.STORED)
{
File file = new File(inputFileName);
FileInputStream f = new FileInputStream(file);
byte[] buff = new byte[f.available()];
f.read(buff);
f.close();
CRC32 crc = new CRC32();
crc.update(buff);
zent.setSize(file.length());
zent.setCrc(crc.getValue());
}
zos.putNextEntry(zent);
readCount = fin.read(buffer);
while (readCount != -1)
{
zos.write(buffer, 0, readCount);
readCount = fin.read(buffer);
}
fin.close();
zos.closeEntry();
}
/**
* Do unzipping by the instance which has been created by the constructor
* JZip(String, Vector).
* @exception IOException if I/O error occurs
*/
public void unzip() throws IOException
{
String inputName = null;
FileInputStream fis = null;
for (int i = 0; i < inputNames.size(); ++i)
{
inputName = (String)inputNames.elementAt(i);
fis = new FileInputStream(inputName);
unzip(fis);
fis.close();
}
}
/**
* Do unzipping by special output path and input stream.
* @exception IOException if I/O error occurs
*/
public void unzip(String outputDir, InputStream is) throws IOException
{
this.outputName = outputDir;
unzip(is);
}
private void unzip(InputStream is) throws IOException
{
FileOutputStream fos;
File file;
ZipInputStream zis = new ZipInputStream(is);
ZipEntry zipEntry = null;
byte[] buffer = new byte[1024];
int readCount = 0;
String outputDirectory;
if (outputName.endsWith(File.separator))
{
outputDirectory = outputName;
}
else
{
outputDirectory = outputName + File.separator;
}while ((zipEntry = zis.getNextEntry()) != null)
{
if (zipEntry.isDirectory())
{
file = new File(outputDirectory + zipEntry.getName());
if (!file.exists())
{
file.mkdir();
}
}
else
{
fos = new FileOutputStream(outputDirectory + zipEntry.getName());
while ((readCount = zis.read(buffer)) != -1)
{
fos.write(buffer, 0, readCount);
}
fos.close();
}
}
zis.close();
}
public static void main(String[] args)
{
Vector zipFileNames = new Vector();
Vector listFileNames = new Vector();
String directoryName = null;
String outputName = null;
String inputName = null;
boolean isZip = true;
boolean isHelp = false;
boolean isCompress = true;
boolean isOption = true;
for (int i = 0; i < args.length; ++i)
{
if (args.equals("-h") || args.equals("-help"))
{
isHelp = true;
}
else if (args.equals("-d"))
{
isZip = false;
}
else if (args.equals("-o"))
{
if ( i == args.length - 1 )
{
isHelp = true;
}
else
{
directoryName = args[++i];
}
}
else if (args.equals("-u"))
{
isCompress = false;
}
else
{
if (isOption)
{
isOption = false;
zipFileNames.addElement(args);
}
else
{
if (isZip)
{
listFileNames.addElement(args);
}
else
{
zipFileNames.addElement(args);
}
}
}
}if (isZip)
{
if ((zipFileNames.size() != 1) || (listFileNames.size()) == 0)
{
isHelp = true;
}
}
else
{
if (zipFileNames.size() == 0)
{
isHelp = true;
}
}if (isHelp)
{
System.out.println("Usage: java JZip [-h] [-d] [-u] [-o <outputDirectory>] zipFile fileList\n" +
" -h display this list\n" +
" -u uncompressed\n" +
" -d unzip\n" +
" -o <outputDirectory> output the files into the 'outputDirectory' directory\n"+
" zipFile zip file name\n" +
" fileList the zipped files list");
}
else
{
try
{
JZip jZip = null;
if (isZip)
{
if (directoryName != null)
{
if (directoryName.endsWith(File.separator))
{
outputName = directoryName + (String)zipFileNames.elementAt(0);
}
else
{
outputName = directoryName + File.separator + (String)zipFileNames.elementAt(0);
}
}
else
{
outputName = (String)zipFileNames.elementAt(0);
}
jZip = new JZip(outputName, listFileNames);
if (isCompress)
{
jZip.zip();
}
else
{
jZip.zipUncompressed();
}
}
else
{
if (directoryName != null)
{
outputName = directoryName;
}
else
{
outputName = System.getProperty("user.dir");
}
jZip = new JZip(outputName, zipFileNames);
jZip.unzip();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}