이 예제는 Java의 Zip 입력 및 출력 스트림을 사용하여 파일을 압축하고 압축합니다. 코드의 이전 부분은 파일 경로 획득, 압축 파일 이름 등의 변경 사항을 구현합니다.
package com.utility.zip; import java.io.bufferedInputStream; import java.io.bufferedoutputStream; import java.io.file; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.fileOutputStream; import compress.io.ioutil.ioutil.ioutil; Java의 zip 입력 및 출력 스트림 * * @author liujiduo * */public final class ziputil {private ziputil () {// empty}/** * 압축 파일 * * @param filepath * 파일 경로를 압축 할 * @return compressed file */public filepath zip (string filepath) {filepath; null; null); (source.exists ()) {// compressed file name = source filename.zipstring zipname = source.getname () + ".zip"; target = new file (source.getparent (), zipname); if (target.exists ()) {target.delete (); wolloutputstream fos = null; null; = 새 파일 OutputStream (target); zos = 새 ZipOutputStream (new bufferedOutputStream (fos)); // 해당 파일 추가 elentAdDentry ( "/", source, zos);} catch (ioexception e) {wrink new runtimeexception (e); {ioutil.closequietly (zos,}}}}}}}} 파일 항목 추가 * * @param base * base path * @param 소스 * 소스 파일 * @param zos * zip 파일 출력 스트림 * @throws ioexception */private static void addentry (문자열 기반, 파일 소스, zipoutputstream zos) ioexception {// (source.isdirectory ()) {for (파일 파일 : source.listfiles ())) {// 디렉토리에 모든 파일을 재귀 적으로 나열하고 EntryAdDentry (Entry + "/", File, Zos);}} else {fileInputStream fis = null; bufferedInputStream bis = null; buffer = [1024 * 10]; fileInputStream (source); bis = new bufferedInputStream (fis, buffer.length); int read = 0; Zos.putnextEntry (new Zipentry (Entry)); while ((read = bis.read (buffer, 0, buffer.length))! = -1) {zos.write (buffer, 0, read);} zos.closeentry ();} 최종 {ioutil.closequietly (bis, fis);}}}}}}}}}}}}}}}; FilePath) {파일 소스 = 새 파일 (FilePath); if (source.exists ()) {zipinputStream zis = null; bufferedOutputStream bos = null; try {zis = new FileInputStream (source)); ZipEntry enthem = null; Entry.GetName ()); if (! target.getParentFile (). zis.read (버퍼, 0, buffer.length))! = -1) {bos.write (buffer, 0, read);} bos.flush ();} zis.closeentry ();} catch (ioexception e) {new runtimeexception (e); main (string [] args) {String targetpath = "e : // win7 Wallpaper"; 파일 파일 = ziputil.zip (targetpath); system.out.println (file); ziputil.unzip ( "f : // win7 wallpaper.zip");}}다음은 IO 스트림 도구 클래스를 통해 하나 이상의 스트림 객체를 닫기위한 Java 언어 설명입니다.
package com.utility.io; import java.io.closeable; import java.io.ioexception;/** * io 스트림 도구 클래스 * * @author liujiduo */public class ioutil {/** * 더 이상의 스트림 객체 * * @param Closeables * @Thatic voidecture (closing voidexception). {if (CloseAbles! = null) {for (Closeable : CloseAbles) {if (closeable! = null) {closeable.close ();}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}/**** @param Closeables* 닫을 수있는 스트림 객체*/public void closequiet (closeable) (closeable). {Close (CloseAbles);} catch (ioException e) {// do nothing}}}요약
위는 Java 압축 파일 도구 Ziputil 사용법의 코드 예제에 대한이 기사의 전체 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오.