Java Unzip Zip- 여러 파일 (폴더 포함), 다음과 같이.
여러 파일과 폴더를 압축하고 복잡한 파일 디렉토리를 압축합니다.
압축 메소드는 가변 매개 변수를 사용하여 1을 여러 파일로 압축 할 수 있습니다. 배열로 작성하거나 매개 변수 목록에 하나씩 쓸 수 있습니다 ...
ZipFiles (Zip, "ABC", 새 파일 ( "d :/earl
파일 디렉토리 구조 테스트 :
테스트의 압축 내용 : 영어 폴더와 동일한 레벨의 두 개의 Excel 파일
file [] files = new File [] {새 파일 ( "d :/English"), 새 파일 ( "d :/versiting data.xls"), 새 파일 ( "d :/inriant name.xls")};압축 코드는 다음과 같습니다.
/ ** * 압축 파일 - 재귀 호출 외부에 있어야하므로 * ZipFiles (ZipoutputStream, String Path, File ... SrcFiles) * @Param Zip * @Param Path * @Param Srcfiles * @throws ioexception * @author isea533 */ public void Zipfiles (파일, 파일, 파일) ioException {zipoutputStream out = new ZipoutPutStream (new FileOutputStream (Zip)); ziptest.zipfiles (out, path, srcfiles); out.close (); System.out.println("*********************************Compressed**********************"); }/*** 압축 파일 파일* @Param ZipFile ZipFile* @param srcfiles 압축 소스 파일* @Author ISEA533*/public static void ZipFiles (ZipoutputStream, 문자열 경로, 파일 ... srcfiles) {path = path.replaceall ( "//", "); if (! path.endswith ( "/")) {path+= "/"; } byte [] buf = 새로운 바이트 [1024]; {for (int i = 0; i <srcfiles.length; i ++) {if (srcfiles [i] .isdirectory ()) {file [] files = srcfiles [i] .listfiles (); 문자열 srcpath = srcfiles [i] .getName (); srcpath = srcpath.replaceall ( "//*", "/"); if (! srcpath.endswith ( "/")) {srcpath+= "/"; } out.putNextEntry (새로운 zipentry (path+srcpath)); zipfiles (out, path+srcpath, 파일); } else {fileInputStream in = new FileInputStream (srcfiles [i]); System.out.println (path + srcfiles [i] .getName ()); out.putnextentry (new Zipentry (path + srcfiles [i] .getname ())); int len; while ((len = in.read (buf))> 0) {out.write (buf, 0, len); } out.closeentry (); 넣다(); }}} catch (예외 e) {e.printstacktrace (); }}압축 중에 폴더를 판단한 다음 파일을 재귀 적으로 압축하십시오.
그런 다음 압축을 풀기 :
/ ** * 지정된 디렉토리에 대한 압축 * @param zippath * @param descdir * @author isea533 */ public static void unzipfiles (String zippath, String descdir)는 ioexception {unzipfiles (새 파일 (zippath), descdir); } / ** * 지정된 디렉토리 * @param ZipFile * @param descdir * @Author ISEA533 * / @suppresswarnings ( "rawtypes") public static void unzipfiles (file zipfile, String descdir) Ioexception 던지기 (descdir); if (! pathfile.exists ()) {pathfile.mkdirs (); } ZipFile Zip = 새로운 ZipFile (ZipFile); for (enumeration entries = zip.getentries (); entries.hasmoreElements ();) {zipentry entry = (zipentry) entries.nextElement (); String ZipentRyName = enty.getName (); inputStream in = zip.getInputStream (Entry); String OutPath = (descdir+zipentryName) .replaceall ( "//*", "/") ;; // 경로가 존재하는지 판단합니다. 존재하지 않으면 파일 경로 파일을 작성하십시오 파일 = 새 파일 (OutPath.SubString (0, outPath.LastIndexOf ( '/'))); if (! file.exists ()) {file.mkdirs (); } // 파일의 전체 경로가 폴더인지 판단합니다. 위에 업로드 된 경우 if (new File (OutPath) .isdirectory ()) {계속; } // 출력 파일 경로 정보 시스템 .out.println (OutPath); outputStream out = 새 FileOutputStream (OutPath); 바이트 [] buf1 = 새로운 바이트 [1024]; int len; while ((len = in.read (buf1))> 0) {out.write (buf1,0, len); } in.close (); out.close (); } System.out.println("*********************************************"); }압축을 눌렀을 때 폴더가 존재하지 않는지 결정하고 폴더 만 생성하지 않고 압축하지 말고 폴더가 아닌 파일을위한 것이기 때문에 폴더가 자체적으로 만들어야하기 때문입니다.
테스트 방법 :
public static void main (string [] args)은 ioexception {/*** compressed file*/file [] files = new File [] {새 파일 ( "d :/English"), 새 파일 ( "d :/export data.xls"), 새 파일 ( "d :/intren name.xls"); 파일 zip = 새 파일 ( "d :/compressed.zip"); zipfiles (zip, "abc", 파일); /*** 압축 파일*/파일 zipfile = 새 파일 ( "d :/compressed.zip"); 문자열 path = "d :/zipfile/"; zipfiles (zipfile, path); }테스트 방법은 예외를 처리하지 않습니다. 이것은 잘못된 것입니다.
출력 결과 :
ABC/English/Templete.xls
ABC/영어/중국어/CSDN/ISEA/533/ABC/Templete.xls
ABC/영어/중국어/CSDN/ISEA/533/ABC/ZipFile2/Templete.xls
ABC/ENGLISC/CINNER/CSDN/ISEA/533/ABC/ZIPFILE2/ZIPFILE/ABC/TEMPLETE.XLS
ABC/ENGLISC/CINNER/CSDN/ISEA/533/ABC/ZIPFILE2/ZIPFILE/ABC/ZIPFILE2/TEMPLETE.XLS
ABC/ENGLISH/CINENTER/CSDN/ISEA/533/ABC/ZIPFILE2/ZIPFILE/ABC/ZIPFILE2/CARD COLLECTION LIST.XLS
ABC/ENGLISH/Chinese/CSDN/ISEA/533/ABC/ZipFile2/Card Collection List.xls
ABC/영어/중국어/CSDN/ISEA/Templete.xls
ABC/영어/중국어/CSDN/ISEA/CARD COLLECT LIST.XLS
ABC/영어/중국어/CSDN/Templete.xls
ABC/ENGLISH/CARD 수신 list.xls
ABC/발행 data.xls
ABC/Chinese Name.xls
********************* 압축 완료 *********************
D :/ZipFile/ABC/Chinese Name.xls
d :/zipfile/abc/issue data.xls
D :/ZipFile/ABC/English/Card 수신 list.xls
d : /zipfile/abc/english/chinese/csdn/templete.xls
D :/ZipFile/ABC/English/Chinese/CSDN/ISEA/Card Collection List.xls
d : /zipfile/abc/english/chinese/csdn/isea/templete.xls
D : /zipfile/abc/english/chinese/csdn/isea/533/abc/templete.xls
d : /zipfile/abc/english/templete.xls
D : /zipfile/abc/english/chinese/csdn/isea/533/abc/zipfile2/templete.xls
d : /zipfile/abc/english/chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
d : /zipfile/abc/english/chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
D :/ZipFile/ABC/English/Chinese/CSDN/ISEA/533/ABC/ZipFile2/ZipFile/ABC/ZipFile2/Card Collection List.xls
D :/ZipFile/ABC/English/Chinese/CSDN/ISEA/533/ABC/ZipFile2/Card-Received List.xls
**************************************
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.