Ant.jar 패키지를 가져와 Apache 웹 사이트 (http://ant.apache.org/bindownload.cgi)에서 다운로드해야합니다.
코드 사본은 다음과 같습니다.
import java.io.bufferedInputStream;
import java.io.bufferedOutputStream;
import java.io.file;
import java.io.fileInputStream;
import java.io.fileoutputStream;
import java.io.ioexception;
import java.util.zip.zipoutputstream;
import org.apache.tools.ant.project;
import org.apache.tools.ant.taskdefs.expand;
import org.apache.tools.zip.zipentry;
import com.xyq.io.util.closeioutil;
공개 수업 ziputil {
개인 정적 최종 문자열 encode = "UTF-8";
public static void zip (String inputFilePath, String ZipFilename) {
파일 inputfile = 새 파일 (inputfilepath);
if (! inputfile.exists ())
새로운 runtimeexception을 던지십시오 ( "원본 파일은 존재하지 않습니다 !!!");
파일 Basetarzipfile = 새 파일 (zipfilename) .getParentFile ();
if (! basetarzipfile.exists () &&! basetarzipfile.mkdirs ())
새로운 runtimeexception을 던지십시오 ( "대상 파일을 생성 할 수 없습니다 !!!");
BufferedOutputStream BOS = NULL;
fileoutputStream out = null;
ZipoutputStream Zout = null;
노력하다 {
// 파일 출력 개체 생성, 프롬프트 : 중국 지원에주의를 기울이십시오.
out = new FileOutputStream (new String (ZipFilename.getBytes (encode)));
BOS = 새로운 BufferedOutputStream (out);
// 파일 출력 스트리밍이 연결되어 있습니다
Zout = 새로운 ZipoutputStream (BOS);
zip (Zout, inputfile, inputfile.getName ());
CloseIoutil.closeall (Zout, Bos, Out);
} catch (예외 e) {
e.printstacktrace ();
}
}
private static void zip (zipoutputstream Zout, 파일 파일, 문자열 base) {
노력하다 {
// 파일 핸들이 디렉토리 인 경우
if (file.isdirectory ()) {
// 디렉토리에서 파일을 가져옵니다
file [] listfiles = file.listfiles ();
// 지퍼 항목을 만듭니다
Zout.putNextEntry (새로운 zipentry (base + "/"));
base = (base.length () == 0? "": base + "/");
if (listfiles! = null && listfiles.length> 0)
// 디렉토리에서 파일을 이동합니다
for (file f : listfiles)
//이 메소드를 재귀 적으로 입력하십시오
zip (Zout, f, base + f.getname ());
}
// 파일 핸들이 파일 인 경우
또 다른 {
if (base == "") {
base = file.getName ();
}
// 파일 핸들을 채우십시오
Zout.putnextentry (새로운 zipentry (기본));
// 압축을 시작합니다
// 파일 유입에서 읽고 지퍼 유출에 쓰십시오
writefile (Zout, File);
}
} catch (예외 e) {
e.printstacktrace ();
}
}
Private STATIC void writefile (ZipoutputStream Zout, 파일 파일)
IoException {{
fileInputStream in = null;
bufferedInputStream bis = null;
in = new FileInputStream (파일);
bis = 새로운 bufferedInputStream (in);
int len = 0;
바이트 [] buff = 새로운 바이트 [2048];
while ((len = bis.read (buff))! = -1)
Zout.write (Buff, 0, Len);
Zout.flush ();
closeioutil.closeall (bis, in);
}
/***
* 감압
*
* @Param Zippath
* zip 파일 경로
* @Param Destination Path
* 감압 대상
* @param ecode
* 인코딩 된 문자 파일 이름 세트
*/
public static void unzip (String Zippath, String DestinationPath) {
파일 zipfile = 새 파일 (zippath);
if (! zipfile.exists ())
새로운 runtimeexception ( "zip 파일" + zippath를 던지십시오
+ "존재하지 않습니다.");
프로젝트 proj = 새로운 프로젝트 ();
확장 확장 = 새로운 확장 ();
expand.setProject (proj);
expand.settasktype ( "unzip");
expand.settaskName ( "unzip");
expand.setsrc (zipfile);
expand.setdest (새 파일 (대상 경로));
expand.setencoding (encode);
expand.execute ();
System.out.println ( "압축 완료 !!!");
}
public static void main (String [] args) {
string dir = new String ( "f : // my 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/");
}
}