파일 전송 프로세스 중에 큰 파일을보다 편리하고 빠른 전송으로 만들기 위해 압축은 일반적으로 전송하기 전에 파일을 압축하는 데 사용됩니다. Java의 java.util.zip 패키지의 Deflater 및 Iplater 클래스는 사용자에게 Deflate 알고리즘의 압축 기능을 제공합니다. 다음은 자체적으로 작성된 압축 및 감압 구현이며 압축 파일 컨텐츠가 예로 들어 있습니다. 관련된 특정 방법은 정보를 위해 JDK API에서 볼 수 있습니다.
/ ** * * @param inputByte * 바이트 배열은 압축 압축 될 * @return 압축 압축 바이트 배열 * @throws ioexception */ public static byte [] uncompress (byte [] inputbyte) IoException {int len = 0; 불타인 염도 = 새로운 인플레이터 (); inf.setinput (inputbyte); BytearRayoutputStream BOS = New ByTearRayoutputStream (); 바이트 [] Outbyte = New Byte [1024]; try {while (! infl.finished ()) {// 압축 압축 및 출력을 바이트 출력 스트림 BOS LEN = infl.Inflate (Outbyte)에 출력합니다. if (len == 0) {break; } bos.write (Outbyte, 0, Len); } infl.end (); } catch (예외 e) {//} 마지막으로 {bos.close (); } return bos.tobytearRay (); } /*** 압축. * * @param inputByte * byte 배열 압축 * @return compressed data * @throws ioexception */ public static byte [] compress (byte [] inputbyte)는 ioexception {int len = 0; deflater defl = New Deflater (); defl.setInput (inputByte); defl.finish (); BytearRayoutputStream BOS = New ByTearRayoutputStream (); 바이트 [] outputByte = 새로운 바이트 [1024]; try {while (! defl.finished ()) {// 압축 된 컨텐츠를 바이트 출력 스트림에 압축하고 출력합니다. bos len = defl.deflate (outputbyte); bos.write (outputbyte, 0, Len); } defl.end (); } 마침내 {bos.close (); } return bos.tobytearRay (); } public static void main (String [] args) {try {fileInputStream fis = new FileInputStream ( "d : //testdeflate.txt"); int len = fis.available (); 바이트 [] B = 새로운 바이트 [Len]; fis.read (b); 바이트 [] bd = compress (b); // 압축 컨텐츠가 네트워크에서 전송되기 위해서는 Base64 인코딩 문자열이 일반적으로 Encodest = base64.encodebase64string (bd)을 인코딩하는 데 사용됩니다. 바이트 [] bi = uncompress (base64.decodebase64 (encodest)); fileoutputStream fos = 새 FileOutputStream ( "d : //testinflate.txt"); fos.write (bi); fos.flush (); fos.close (); fis.close (); } catch (예외 e) {//}}Java의 위의 압축 구현 방법은 내가 공유 한 모든 컨텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.