ant.jarパッケージをインポートし、Apache Webサイト(http://ant.apache.org/bindownload.cgi)にダウンロードする必要があります。
コードコピーは次のとおりです。
java.io.bufferedinputStreamをインポートします。
java.io.bufferedOutputStreamをインポートします。
java.io.fileをインポートします。
java.io.fileinputStreamをインポートします。
java.io.fileoutputStreamをインポートします。
java.io.ioexceptionをインポートします。
java.util.zip.zipoutputStreamをインポートします。
org.apache.tools.ant.projectをインポートします。
org.apache.tools.ant.taskdefs.expandをインポートします。
org.apache.tools.zip.zipentryをインポートします。
com.xyq.io.util.closeioutilをインポートします。
パブリッククラスziputil {
private static final string encode = "utf-8";
public static void zip(string inputfilepath、string zipfilename){
ファイルinputFile = new File(inputFilePath);
if(!inputfile.exists())
新しいruntimeexception( "元のファイルは存在しません!!!");
ファイルBasetarzipfile = new file(zipfilename).getParentFile();
if(!basetarzipfile.exists()&&!basetarzipfile.mkdirs()))
新しいruntimeexception( "ターゲットファイルを作成できません!!!");
BufferedOutputStream bos = null;
fileoutputStream out = null;
zipoutputStream zout = null;
試す {
//ファイル出力オブジェクトを作成する、プロンプト:中国のサポートに注意を払う
out = new fileoutputStream(new String(zipfileName.getBytes(embode)));
bos = new BufferedOutputStream(out);
//ファイル出力ストリーミングが接続されています
zout = new zipoutputStream(bos);
zip(zout、inputfile、inputfile.getname());
closeioutil.closeall(Zout、bos、out);
} catch(例外e){
e.printstacktrace();
}
}
private static void zip(zipoutputstream zout、file file、string base){
試す {
//ファイルハンドルがディレクトリの場合
if(file.isdirectory()){
//ディレクトリでファイルを取得します
file [] listfiles = file.listfiles();
// zipエントリを作成します
zout.putnextentry(new zipentry(base + "/"));
base =(base.length()== 0? "":base + "/");
if(listfiles!= null && listfiles.length> 0)
//ディレクトリ内のファイルを移動します
for(ファイルf:listfiles)
//この方法を再帰的に入力します
zip(zout、f、base + f.getname());
}
//ファイルハンドルがファイルの場合
それ以外 {
if(base == ""){
base = file.getName();
}
//ファイルハンドルに入力します
zout.putnextentry(new zipentry(base));
//圧縮を開始します
//ファイルの流入から読み、zip流出に書き込みます
writefile(zout、file);
}
} catch(例外e){
e.printstacktrace();
}
}
private static void writefile(zipoutputStream zout、ファイルファイル)
IOExceptionをスローする{
fileInputStream in = null;
bufferedinputStream bis = null;
in = new FileInputStream(file);
bis = new BufferedInputStream(in);
int len = 0;
byte [] buff = new byte [2048];
while((len = bis.read(buff))!= -1)
zout.write(buff、0、len);
zout.flush();
closeioutil.closeall(bis、in);
}
/***
*減圧
*
* @param zippath
*ファイルパスをzip
* @Param DestinationPath
*減圧の宛先
* @param ecode
*ファイル名のエンコードされた文字セット
*/
public static void unzip(string zippath、string destinationpath){
ファイルzipfile = new file(zippath);
if(!zipfile.exists())
新しいruntimeexception( "zip file" + zippathを投げます
+「存在しません。 ");
Project proj = new Project();
展開展開= new Expand();
Expand.setProject(proj);
Expand.SettaskType( "Unzip");
Expand.settaskname( "unzip");
Expand.setsrc(zipfile);
Expand.setDest(new file(destinationPath));
Expand.SetENCODING(エンコード);
Expand.execute();
system.out.println( "unzip done !!!");
}
public static void main(string [] args){
string dir = new String( "f:// backup // document // myeclipse+9.0公式バージョンのクラックとアクティベーション(プロフェッショナルテスト利用可能)");
dir = new String( "f:/111.jpg");
zip(dir、 "f:/bzbxb/zipant.zip");
unzip( "f:/bzbxb/zipant.zip"、 "f:/xx/xx/");
}
}