Using Java to implement Zip compressed file and directory program code (b) __java

Source: Internet
Author: User
Tags cos crc32

Today wrote a Java compression function, you can achieve the compression of files and directories.

Because Java.util.zip.ZipOutputStream has the Chinese garbled problem, therefore uses the Org.apache.tools.zip.ZipOutputStream.
Here's the code:
Java code
Package net.szh.zip;

Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.zip.CRC32;
Import Java.util.zip.CheckedOutputStream;

Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream;

public class Zipcompressor {
static final int BUFFER = 8192;

Private File ZipFile;

Public Zipcompressor (String pathName) {
ZipFile = new File (pathName);
}

public void compress (String srcpathname) {
File File = new file (srcpathname);
if (!file.exists ())
throw new RuntimeException (Srcpathname + "does not exist.) ");
try {
FileOutputStream FileOutputStream = new FileOutputStream (ZipFile);
Checkedoutputstream cos = new Checkedoutputstream (FileOutputStream,
New CRC32 ());
Zipoutputstream out = new Zipoutputstream (COS);
String basedir = "";
Compress (file, out, basedir);
Out.close ();
catch (Exception e) {
throw new RuntimeException (e);
}
}

private void compress (file file, zipoutputstream out, String basedir) {
/* Determine whether the directory or file * *
if (File.isdirectory ()) {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressdirectory (file, out, basedir);
} else {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressfile (file, out, basedir);
}
}

/** Compress a directory * *
private void Compressdirectory (File dir, zipoutputstream out, String basedir) {
if (!dir.exists ())
Return

file[] files = dir.listfiles ();
for (int i = 0; i < files.length; i++) {
/* Recursive * *
Compress (Files[i], out, Basedir + dir.getname () + "/");
}
/** Compress a file */
private void Compressfile (file file, zipoutputstream out, String basedir) {
if (!file.exists ()) {
Return
}
try {
Bufferedinputstream bis = new Bufferedinputstream (
New FileInputStream (file));
ZipEntry entry = new ZipEntry (Basedir + file.getname ());
Out.putnextentry (entry);
int count;
byte data[] = new Byte[buffer];
while (count = bis.read (data, 0, BUFFER)!=-1) {
Out.write (data, 0, count);
}
Bis.close ();
catch (Exception e) {
throw new RuntimeException (e);
}
}
}

Package net.szh.zip;

Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.zip.CRC32;
Import Java.util.zip.CheckedOutputStream;

Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream;

public class Zipcompressor {
static final int BUFFER = 8192;

Private File ZipFile;

Public Zipcompressor (String pathName) {
ZipFile = new File (pathName);
}

public void compress (String srcpathname) {
File File = new file (srcpathname);
if (!file.exists ())
throw new RuntimeException (Srcpathname + "does not exist.) ");
try {
FileOutputStream FileOutputStream = new FileOutputStream (ZipFile);
Checkedoutputstream cos = new Checkedoutputstream (FileOutputStream,
New CRC32 ());
Zipoutputstream out = new Zipoutputstream (COS);
String basedir = "";
Compress (file, out, basedir);
Out.close ();
catch (Exception e) {
throw new RuntimeException (e);
}
}

private void compress (file file, zipoutputstream out, String basedir) {
/* Determine whether the directory or file * *
if (File.isdirectory ()) {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressdirectory (file, out, basedir);
} else {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressfile (file, out, basedir);
}
}

/** Compress a directory * *
private void Compressdirectory (File dir, zipoutputstream out, String basedir) {
if (!dir.exists ())
Return

file[] files = dir.listfiles ();
for (int i = 0; i < files.length; i++) {
/* Recursive * *
Compress (Files[i], out, Basedir + dir.getname () + "/");
}
/** Compress a file */
private void Compressfile (file file, zipoutputstream out, String basedir) {
if (!file.exists ()) {
Return
}
try {
Bufferedinputstream bis = new Bufferedinputstream (
New FileInputStream (file));
ZipEntry entry = new ZipEntry (Basedir + file.getname ());
Out.putnextentry (entry);
int count;
byte data[] = new Byte[buffer];
while (count = bis.read (data, 0, BUFFER)!=-1) {
Out.write (data, 0, count);
}
Bis.close ();
catch (Exception e) {
throw new RuntimeException (e);
}
}
}

It turns out that you can use Ant's Org.apache.tools.ant.taskdefs.Zip to make it easier.
Java code
Package net.szh.zip;

Import Java.io.File;

Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Zip;
Import Org.apache.tools.ant.types.FileSet;

public class Zipcompressorbyant {

Private File ZipFile;

Public zipcompressorbyant (String pathName) {
ZipFile = new File (pathName);
}

public void compress (String srcpathname) {
File Srcdir = new file (srcpathname);
if (!srcdir.exists ())
throw new RuntimeException (Srcpathname + "does not exist.) ");

Project PRJ = new Project ();
Zip zip = new zip ();
Zip.setproject (PRJ);
Zip.setdestfile (ZipFile);
Fileset fileset = new Fileset ();
Fileset.setproject (PRJ);
Fileset.setdir (Srcdir);
Fileset.setincludes ("**/*.java"); Include which files or folders eg:zip.setIncludes ("*.java");
Fileset.setexcludes (...); Which files or folders are excluded
Zip.addfileset (Fileset);

Zip.execute ();
}
}

Package net.szh.zip;

Import Java.io.File;

Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Zip;
Import Org.apache.tools.ant.types.FileSet;

public class Zipcompressorbyant {

Private File ZipFile;

Public zipcompressorbyant (String pathName) {
ZipFile = new File (pathName);
}

public void Compress (String srcpathname) {
File srcdir = new file (srcpathname);
if (!srcdir.exists ())
Throw The new RuntimeException (Srcpathname + "does not exist.) ");

Project PRJ = new Project ();
Zip zip = new zip ();
Zip.setproject (PRJ);
Zip.setdestfile (ZipFile);
Fileset fileset = new Fileset ();
Fileset.setproject (PRJ);
Fileset.setdir (Srcdir);
//fileset.setincludes ("**/*.java"), including which files or folders eg:zip.setIncludes ("*.java");
//fileset.setexcludes (...); Which files or folders to exclude
Zip.addfileset (fileset);

Zip.execute ();
}
}
Test
Java Code
Package Net.szh.zip public class Testzip {
public static void Main (string[) a RGS) {
Zipcompressor ZC = new Zipcompressor ("E:" "Szhzip.zip");
Zc.compress ("E:" "Test");

Zipcompressorbyant zca = new Zipcompressorbyant ("E:" "Szhzipant.zip");
Zca.compress ("E:" "Test");
}
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.