Java Basicクラスを使用して書かれた簡単なzip圧縮減圧ツールクラス
コードコピーは次のとおりです。
パッケージsun.net.helper;
java.io.*をインポートします。
java.util.logging.loggerをインポートします。
java.util.zip。*をインポートします。
パブリッククラスziputil {
プライベート最終静的ロガーロガー= logger.getLogger(ziputil.class.getName());
プライベート静的最終intバッファー= 1024*10;
/**
*指定されたディレクトリをディレクトリと同じ名前のzipファイルに圧縮し、圧縮パスをカスタマイズします
* @param sourcefilepathターゲットファイルパス
* @param zipfilepath zipファイルパスを指定します
* @戻る
*/
public static boolean zip(string sourcefilepath、string zipfilepath){
boolean result = false;
ファイルsource = new file(sourcefilepath);
if(!source.exists()){
logger.info(sourcefilepath+"not not inted。");
返品結果;
}
if(!source.isdirectory()){
logger.info(sourcefilepath+"はディレクトリではありません。");
返品結果;
}
ファイルzipfile = new file(zipfilepath+"/"+source.getName()+"。zip");
if(zipfile.exists()){
logger.info(zipfile.getName()+"は既に存在しています。");
返品結果;
}それ以外{
if(!zipfile.getParentFile()。exists()){
if(!zipfile.getParentFile()。mkdirs()){
logger.info( "ファイルを作成できません"+zipfile.getName());
返品結果;
}
}
}
logger.info( "zipファイルの作成...");
fileoutputStream dest = null;
zipoutputStream out = null;
試す {
dest = new fileoutputStream(zipfile);
checkedOutputStream checksum = new checkedOutputStream(dest、new Adler32());
out = new zipoutputStream(new BufferedOutputStream(Checksum));
out.setMethod(zipoutputStream.deflated);
Compress(source、out、source.getname());
result = true;
} catch(filenotfoundexception e){
e.printstacktrace();
} ついに {
if(out!= null){
試す {
out.closeentry();
} catch(ioException e){
e.printstacktrace();
}
試す {
out.close();
} catch(ioException e){
e.printstacktrace();
}
}
}
if(result){
logger.info( "done。");
}それ以外{
logger.info( "fail。");
}
返品結果;
}
private static void Compress(ファイルファイル、zipoutputStream out、string mainfilename){
if(file.isfile()){
fileinputStream fi = null;
bufferedinputStream origin = null;
試す {
fi = new fileinputStream(file);
Origin = new BufferedInputStream(FI、バッファー);
int index = file.getabsolutepath()。indexof(mainfilename);
string entryname = file.getabsolutepath()。substring(index);
System.out.println(entryName);
zipentry entry = new Zipentry(entryName);
out.putnextentry(entry);
byte [] data = new byte [buffer];
int count;
while((count = origin.read(data、0、buffer))!= -1){
out.write(data、0、count);
}
} catch(filenotfoundexception e){
e.printstacktrace();
} catch(ioException e){
e.printstacktrace();
} ついに {
if(origin!= null){
試す {
Origin.Close();
} catch(ioException e){
e.printstacktrace();
}
}
}
} else if(file.isdirectory()){
file [] fs = file.listfiles();
if(fs!= null && fs.length> 0){
for(file f:fs){
Compress(f、out、mainfilename);
}
}
}
}
/**
* zipファイルを指定されたディレクトリに解凍する必要があります。
* @param zipfile
* @Param DestPath
* @戻る
*/
public static boolean unzip(file zipfile、string destpath){
boolean result = false;
if(!zipfile.exists()){
logger.info(zipfile.getname()+"not inted。");
返品結果;
}
ファイルターゲット= newファイル(DestPath);
if(!target.exists()){
if(!target.mkdirs()){
logger.info( "ファイルを作成できません"+target.getName());
返品結果;
}
}
string mainfilename = zipfile.getName()。falple( "。zip"、 "");
ファイルターゲットファイル= newファイル(DestPath+"/"+MainFileName);
if(targetfile.exists()){
logger.info(targetfile.getName()+"既に存在します。");
返品結果;
}
zipinputStream zis = null;
logger.info( "unzipファイルを開始...");
試す {
fileInputStream fis = new fileInputStream(zipfile);
CheckedInputStream checksum = new CheckedInputStream(fis、new Adler32());
zis = new ZipinputStream(new BufferedInputStream(Checksum));
Zipentryエントリ;
while((entry = zis.getnextentry())!= null){
int count;
バイトデータ[] = new byte [buffer];
string entryName = entry.getName();
int index = entryname.indexof(mainfilename);
文字列newEntryName = destPath+"/"+entryName.substring(index);
System.out.println(newEntryName);
file temp = new file(newEntryName).getParentFile();
if(!temp.exists()){
if(!temp.mkdirs()){
新しいruntimeexception( "file" +temp.getName() +"fail");
}
}
fileoutputStream fos = new fileoutputStream(newEntryName);
BufferedOutputStream dest = new BufferedOutputStream(FOS、バッファー);
while((count = zis.read(data、0、buffer))!= -1){
dest.write(data、0、count);
}
dest.flush();
dest.close();
}
result = true;
} catch(filenotfoundexception e){
e.printstacktrace();
} catch(ioException e){
e.printstacktrace();
} ついに {
if(zis!= null){
試す {
zis.close();
} catch(ioException e){
e.printstacktrace();
}
}
}
if(result){
logger.info( "done。");
}それ以外{
logger.info( "fail。");
}
返品結果;
}
public static void main(string [] args)throws ioexception {
//ziputil.zip("d:/apache-tomcat-7.0.30 "、" d:/temp ");
ファイルzipfile = new file( "d:/temp/apache-tomcat-7.0.30.zip");
ziputil.unzip(zipfile、 "d:/temp");
}
}
別の圧縮と減圧の例は、2つのツールを参照してください。
コードコピーは次のとおりです。
パッケージcom.lanp;
java.io.fileをインポートします。
java.io.fileinputStreamをインポートします。
java.io.fileoutputStreamをインポートします。
java.io.ioexceptionをインポートします。
java.util.zip.zipentryをインポートします。
java.util.zip.zipexceptionをインポートします。
java.util.zip.zipfileをインポートします。
java.util.zip.zipinputStreamをインポートします。
/**
* zip圧縮ファイルを指定されたディレクトリに解凍します
*/
パブリックファイナルクラスZiptofile {
/**
*キャッシュサイズのデフォルトは20480です
*/
プライベート最終static int file_buffer_size = 20480;
private ziptofile(){
}
/**
*指定されたディレクトリのzip圧縮ファイルを指定されたディレクトリに解凍します
* @param zipfilepathzip圧縮ファイルパス
* @param zipfilenamezip圧縮ファイル名
* @Param TargetFileDirzip圧縮ファイルを抽出します
* @return flag boolean戻り値
*/
public static boolean unzip(string zipfilepath、string zipfilename、string targetfiledir){
ブールフラグ= false;
// 1。圧縮ファイルが存在するかどうか、
ファイルファイル= null; //圧縮ファイル(パス付き)
zipfile zipfile = null;
file = new file(zipfilepath + "/" + zipfilename);
System.out.println( ">>>>>>>ファイルを解凍します
if(false == file.exists()){
System.out.println( ">>>>>>>圧縮ファイル[" + zipfilepath + "/" + zipfileName + "]は存在しません<<<<<<<");
falseを返します。
} else if(0 == file.length()){
System.out.println( ">>>>>>>圧縮ファイル[" + zipfilepath + "/" + zipfileName + "]サイズは0で、減圧<<<<<<");
falseを返します。
} それ以外 {
// 2
byte [] buf = new byte [file_buffer_size];
int readsize = -1;
zipinputStream zis = null;
fileoutputStream fos = null;
試す {
// zipファイルのかどうかを確認します
zipfile = new zipfile(file);
zipfile.close();
//ターゲットディレクトリが存在しない場合は、作成します
ファイルnewDir = new File(TargetFileDir);
if(false == newdir.exists()){
newdir.mkdirs();
newdir = null;
}
zis = new ZipinputStream(new FileInputStream(file));
zipentry zipentry = zis.getnextentry();
//圧縮パッケージ内のファイルの処理を開始します
while(null!= zipentry){
string zipentryname = zipentry.getname()。置換( '//'、 '/');
// Zipentryがディレクトリであるかどうかを判断します
if(zipentry.isdirectory()){
int indexnumber = zipentryname.lastindexof( '/');
file entrydirs = new file(targetfiledir + "/" + zipentryname.substring(0、indexNumber));
entrydirs.mkdirs();
EntryDirs = null;
} それ以外 {
試す {
fos = new fileoutputStream(targetfiledir + "/" + zipentryname);
while((readsize = zis.read(buf、0、file_buffer_size))!= -1){
fos.write(buf、0、readsize);
}
} catch(例外e){
e.printstacktrace();
新しいruntimeexception(e.getCause());
} ついに {
試す {
if(null!= fos){
fos.close();
}
} catch(ioException e){
e.printstacktrace();
新しいruntimeexception(e.getCause());
}
}
}
zipentry = zis.getnextentry();
}
flag = true;
} catch(zipexception e){
e.printstacktrace();
新しいruntimeexception(e.getCause());
} catch(ioException e){
e.printstacktrace();
新しいruntimeexception(e.getCause());
} ついに {
試す {
if(null!= zis){
zis.close();
}
if(null!= fos){
fos.close();
}
} catch(ioException e){
e.printstacktrace();
新しいruntimeexception(e.getCause());
}
}
}
フラグを返します。
}
/**
*テストの主な方法
*/
public static void main(string [] args){
string zipfilepath = "c:// home";
string zipfilename = "lp20120301.zip";
String TargetFileDir = "C:// home // lp20120301";
boolean flag = ziptofile.unzip(zipfilepath、zipfilename、targetfiledir);
if(flag){
System.out.println( ">>>>>>> <<<<<<<");
} それ以外 {
System.out.println( ">>>>>>>>減圧が失敗しました<<<<<<");
}
}
}