Kelas alat dekompresi kompresi zip sederhana yang ditulis menggunakan kelas dasar java
Salinan kode adalah sebagai berikut:
paket sun.net.helper;
impor java.io.*;
impor java.util.logging.logger;
impor java.util.zip.*;
Ziputil kelas publik {
private final static logger = logger.getLogger (ziputil.class.getName ());
private static final int buffer = 1024*10;
/**
* Kompres direktori yang ditentukan ke file zip dengan nama yang sama dengan direktori, dan sesuaikan jalur kompresi
* @param SourceFilePath Target File Path
* @param zipfilePath Tentukan jalur file zip
* @kembali
*/
zip boolean statis publik (string sourceFilePath, string zipfilePath) {
Hasil boolean = false;
Sumber file = file baru (SourceFilePath);
if (! source.exists ()) {
logger.info (SourceFilePath+"tidak ada.");
hasil pengembalian;
}
if (! source.isdirectory ()) {
logger.info (SourceFilePath+"bukan direktori.");
hasil pengembalian;
}
File zipfile = file baru (zipfilePath+"/"+source.getName ()+". Zip");
if (zipfile.exists ()) {
logger.info (zipfile.getName ()+"sudah ada.");
hasil pengembalian;
}kalau tidak{
if (! zipfile.getParentFile (). Exists ()) {
if (! zipfile.getParentFile (). mkdirs ()) {
logger.info ("tidak dapat membuat file"+zipfile.getName ());
hasil pengembalian;
}
}
}
logger.info ("Membuat file zip ...");
FileOutputStream dest = null;
ZipoutputStream out = null;
mencoba {
dest = new fileoutputStream (zipfile);
CheckedOutputStream checksum = new checkedoutputStream (dest, new adler32 ());
out = new zipoutputStream (baru bufferedoutputStream (checksum));
out.setMethod (zipoutputStream.deflated);
kompres (sumber, out, source.getName ());
Hasil = Benar;
} catch (FileNotFoundException e) {
e.printstacktrace ();
} Akhirnya {
if (out! = null) {
mencoba {
out.closeentry ();
} catch (ioException e) {
e.printstacktrace ();
}
mencoba {
out.close ();
} catch (ioException e) {
e.printstacktrace ();
}
}
}
if (result) {
logger.info ("Done.");
}kalau tidak{
logger.info ("gagal.");
}
hasil pengembalian;
}
private static void compress (file file, zipoutputStream out, string mainfileName) {
if (file.isfile ()) {
FileInputStream fi = null;
BufferedInputStream Origin = null;
mencoba {
fi = FileInputStream baru (file);
Origin = new bufferedInputStream (fi, buffer);
int index = file.getAbsolutePath (). IndexOf (MainFileName);
String entryName = file.getAbsolutePath (). Substring (index);
System.out.println (entryname);
Entri zipentry = zipentry baru (entriName);
out.putNextEntry (entri);
byte [] data = byte baru [buffer];
jumlah int;
while ((count = origin.read (data, 0, buffer))! = -1) {
out.write (data, 0, count);
}
} catch (FileNotFoundException e) {
e.printstacktrace ();
} catch (ioException e) {
e.printstacktrace ();
} Akhirnya {
if (asal! = null) {
mencoba {
Origin.close ();
} catch (ioException e) {
e.printstacktrace ();
}
}
}
} lain jika (file.isdirectory ()) {
File [] fs = file.listFiles ();
if (fs! = null && fs.length> 0) {
untuk (file f: fs) {
kompres (f, out, mainfileName);
}
}
}
}
/**
* Unzip file zip ke direktori yang ditentukan.
* @param zipfile
* @param destpath
* @kembali
*/
Public Static Boolean Unzip (File Zipfile, String DestPath) {
Hasil boolean = false;
if (! zipfile.exists ()) {
logger.info (zipfile.getName ()+"tidak ada.");
hasil pengembalian;
}
File target = file baru (destPath);
if (! target.exists ()) {
if (! target.mkdirs ()) {
logger.info ("tidak dapat membuat file"+target.getName ());
hasil pengembalian;
}
}
String MainFileName = zipFile.getName (). REPLACE (". ZIP", "");
File targetFile = file baru (destpath+"/"+mainfileName);
if (targetfile.exists ()) {
logger.info (targetfile.getName ()+"sudah ada.");
hasil pengembalian;
}
ZipInputStream zis = null;
logger.info ("Mulai file unzip ...");
mencoba {
FileInputStream fis = FileInputStream baru (ZipFile);
CheckedInputStream checksum = new checkedInputStream (FIS, new adler32 ());
zis = zipinputStream baru (baru bufferedInputStream (checksum));
Entri zipentry;
while ((entry = zis.getNextEntry ())! = null) {
jumlah int;
data byte [] = byte baru [buffer];
String entryname = entry.getName ();
int index = entryname.indexof (mainfileName);
String newEntryname = destPath+"/"+entryname.substring (index);
System.out.println (newEntryname);
File temp = file baru (newEntryname) .getParentFile ();
if (! temp.exists ()) {
if (! temp.mkdirs ()) {
Lempar RunTimeException baru ("Buat File" +Temp.GetName () +"Fail");
}
}
FileOutputStream fos = FileOutputStream baru (newEntryName);
BufferedOutputStream dest = baru bufferedOutputStream (fos, buffer);
while ((count = zis.read (data, 0, buffer))! = -1) {
dest.write (data, 0, count);
}
dest.flush ();
dest.close ();
}
Hasil = Benar;
} catch (FileNotFoundException e) {
e.printstacktrace ();
} catch (ioException e) {
e.printstacktrace ();
} Akhirnya {
if (zis! = null) {
mencoba {
zis.close ();
} catch (ioException e) {
e.printstacktrace ();
}
}
}
if (result) {
logger.info ("Done.");
}kalau tidak{
logger.info ("gagal.");
}
hasil pengembalian;
}
public static void main (string [] args) melempar ioException {
//Ziputil.zip("d:/apache-tomcat-7.0.30 "," d:/temp ");
File zipfile = file baru ("d: /temp/apache-tomcat-7.0.30.zip");
Ziputil.unzip (zipfile, "d:/temp");
}
}
Contoh kompresi dan dekompresi lainnya, silakan merujuk ke dua alat.
Salinan kode adalah sebagai berikut:
paket com.lanp;
impor java.io.file;
impor java.io.fileInputStream;
impor java.io.fileoutputStream;
impor java.io.ioException;
impor java.util.zip.zipentry;
impor java.util.zip.zipException;
impor java.util.zip.zipfile;
impor java.util.zip.zipinputStream;
/**
* Unzip file terkompresi zip ke direktori yang ditentukan
*/
Kelas Akhir Publik Ziptofile {
/**
* Ukuran cache default ke 20480
*/
private final static int file_buffer_size = 20480;
private ziptofile () {
}
/**
* Unzip file terkompresi zip dari direktori yang ditentukan ke direktori yang ditentukan
* @param zipfilePathzip path file terkompresi
* @param zipfilenamezip nama file terkompresi
* @param TargetFileRzip file terkompresi untuk diekstraksi
* @return Flag Boolean Return Nilai
*/
Public Static Boolean Unzip (String ZipFilePath, String ZipFileName, String TargetFiledir) {
bendera boolean = false;
// 1.
File file = null; // file terkompresi (dengan path)
ZipFile Zipfile = null;
file = file baru (zipfilePath + "/" + zipfileName);
System.out.println (">>>>>>> Buka zip file [" + ZipFilePath + "/" + ZipFileName + "] ke direktori [" + targetfiledir + "] <<<<<<<<");
if (false == file.exists ()) {
System.out.println (">>>>>>> file terkompresi [" + zipfilePath + "/" + zipfileName + "] tidak ada <<<<<<<");
mengembalikan false;
} else if (0 == file.length ()) {
System.out.println (">>>>>>> file terkompresi [" + zipfilePath + "/" + zipfileName + "] ukurannya 0 dan tidak memerlukan dekompresi <<<<<<");
mengembalikan false;
} kalau tidak {
// 2.
byte [] buf = byte baru [file_buffer_size];
int readsize = -1;
ZipInputStream zis = null;
FileOutputStream fos = null;
mencoba {
// Periksa apakah itu file zip
zipfile = zipfile baru (file);
zipfile.close ();
// Tentukan apakah ada direktori target.
File newdir = file baru (targetfiledir);
if (false == newDir.exists ()) {
newdir.mkdirs ();
newdir = null;
}
zis = new zipInputStream (FileInputStream baru (file));
Zipentry zipentry = zis.getNextEntry ();
// Mulai memproses file dalam paket terkompresi
while (null! = zipentry) {
String zipEntryname = zipEntry.getName (). Ganti ('//', '/');
// menilai apakah zipentry adalah direktori
if (zipentry.isdirectory ()) {
int indexNumber = zipEntryname.LastIndexOf ('/');
File entrydirs = file baru (targetfiledir + "/" + zipentryname.substring (0, indexNumber));
entrydirs.mkdirs ();
entrydirs = null;
} kalau tidak {
mencoba {
fos = fileoutputStream baru (targetfiledir + "/" + zipentryname);
while ((readsize = zis.read (buf, 0, file_buffer_size))! = -1) {
fos.write (buf, 0, readsize);
}
} catch (Exception e) {
e.printstacktrace ();
Lempar RuntimeException baru (e.getCause ());
} Akhirnya {
mencoba {
if (null! = fos) {
fos.close ();
}
} catch (ioException e) {
e.printstacktrace ();
Lempar RuntimeException baru (e.getCause ());
}
}
}
zipentry = zis.getNextEntry ();
}
bendera = true;
} catch (ZipException e) {
e.printstacktrace ();
Lempar RuntimeException baru (e.getCause ());
} catch (ioException e) {
e.printstacktrace ();
Lempar RuntimeException baru (e.getCause ());
} Akhirnya {
mencoba {
if (null! = zis) {
zis.close ();
}
if (null! = fos) {
fos.close ();
}
} catch (ioException e) {
e.printstacktrace ();
Lempar RuntimeException baru (e.getCause ());
}
}
}
pengembalian bendera;
}
/**
* Metode utama untuk pengujian
*/
public static void main (string [] args) {
String zipfilePath = "c: // home";
String zipfileName = "lp20120301.zip";
String targetfiledir = "c: // home // lp20120301";
bendera boolean = ziptofile.unzip (zipfilePath, zipfileName, targetfiledir);
if (flag) {
System.out.println (">>>>>>> Decompress berhasil <<<<<<<<");
} kalau tidak {
System.out.println (">>>>>>> Dekompresi gagal <<<<<<");
}
}
}