패키지 com.badboyh2o.sduty.apacheCommonsCompres; 수입 java.io.BufferedInputStream의; 수입 java.io.BufferedOutputStream에; 수입 java.io.ByteArrayInputStream의; 수입 java.io.ByteArrayOutputStream의; 수입 java.io.File에; 수입 java.io.FileInputStream의; 수입 java.io.FileNotFoundException; 수입 java.io.FileOutputStream의; 수입 때 java.io.IOException; 수입 java.io.InputStream를; 수입 java.io.OutputStream에; 수입 java.util.Iterator를; 수입 org.apache.commons.compress.archivers.ArchiveEntry; 수입 org.apache.commons.compress.archivers.ArchiveException; 수입 org.apache.commons.compress.archivers.ArchiveOutputStream; 수입 org.apache.commons.compress.archivers.ArchiveStreamFactory; 수입 org.apache.commons.compress.archivers.zip.ZipArchiveEntry; 수입 org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; 수입 org.apache.commons.io.FileUtils; 수입 org.apache.commons.io.IOUtils; 공공 클래스 의 압축을 { 공공 정적 무효 메인 (문자열 []에 인수) 가 발생 FileNotFoundException이, IOException가 { //decompressZip (getFileByteArray ( "G : /test/1.zip"), "G : 압축 풀기 / /"), ZIP ( "G : /test/2.zip", "G : 압축 풀기 /" ) } / ** * * @param의 압축을 DestFile /opt/nfs/1.zip 같은 파일 경로 * @param의 파일 / TEMP으로 위치한 압축되어야 zipDir 경로 * / 공공 정적 무효 ZIP (DestFile이 문자열 zipDir 문자열) { OUTFILE 파일 = 새 새 파일합니다 (DestFile이) 은 try { outFile.createNewFile (); // 파일 생성 의 BufferedOutputStream의 BufferedOutputStream = 새로운 새 의 BufferedOutputStream (새로운 FileOutputStream에 (OUTFILE)); 밖으로 ArchiveOutputStream = 새로운 ArchiveStreamFactory () createArchiveOutputStream (ArchiveStreamFactory.JAR. 의 BufferedOutputStream); 경우 (zipDir.charAt (zipDir.length () - 1)! = File.separatorChar) { zipDir + = File.separatorChar; } 반복자 <파일> 파일 =의 FileUtils.iterateFiles ( 새 파일 (zipDir), 널 (null) , 진정한 ); 동안 (files.hasNext ()) { 파일 파일 = files.next (); ZipArchiveEntry zipArchiveEntry = 새 ZipArchiveEntry (파일, file.getPath () 대체 (zipDir.replace (. "/", "\\"), "" )); out.putArchiveEntry (zipArchiveEntry); IOUtils.copy ( 새 FileInputStream에 (파일), 아웃); out.closeArchiveEntry (); } out.finish (); out.close (); } 캐치 (IOException이 전자) { System.err.println을 ( "创建文件失败" ); } 캐치 (ArchiveException 전자) { System.err에."지원되지 않는 압축 형식" ); } } / ** * zip 파일 바이트 [] 지정된 폴더를 추출 * * 하나 개의 압축 파일 지원 * * @param ZipFile에 * @param 감압 후 saveFileDir을 같은 파일 저장 경로, "D / 테스트 /" * @throws IOException가 *는 @throws FileNotFoundException이 * / 공공 정적 무효 decompressZip ( 바이트 [] ZipFile를 문자열 saveFileDir) 드로우 {FileNotFoundException이 IOException을 InputStream 인 인 = 널 ; Zipis ZipArchiveInputStream = 널 ; 은 try는 { IS = 새로운 새 BufferedInputStream을 ( 새로운 새 ;이 InputStream (ZipFile에)) zipis = 새로운 새 ZipArchiveInputStream (IS) ArchiveEntry archiveEntry = 널 ; // zip 파일은 각각의 패킷을 판독 // 다음 파일 지정된 폴더에 쓰기 그동안 ((archiveEntry = zipis.getNextEntry ())! = null이 ) { // 파일 이름 얻을 문자열을 entryFileName =을archiveEntry.getName (); // 구성 파일 복원 스토리지 경로 문자열 entryFilePath + = saveFileDir entryFileName; 바이트 [] 함량 = 새로운 새 바이트 [( INT ) archiveEntry.getSize ()]; zipis.read (콘텐츠) OutputStream에 OS = 널 (null) ; 은 try { // 지정된 경로에 기록 된 파일을 압축 해제 파일 entryFile = 새 새 ; 파일 (entryFilePath) // 에서 System.out.println (archiveEntry.getSize ()); 에서 System.out.println (entryFilePath); OS = 새 의 BufferedOutputStream ( 신규 의 FileOutputStream (entryFile)); os.write (콘텐츠); } 마지막으로 { 경우 (OS! = null이 ) { os.flush (); os.close (); } } } } 마지막으로 { 시도 { 경우 (! zipis = 널 (null) ) { zipis.close (); } 경우(인 =! 널 ) { is.close (); } } 캐치 (IOException이 전자) { 던져 새로운 RuntimeException을 (전자); } } } / ** *测试将文件转为바이트 [] * * @param 적인 filePath * @return * / 공용 정적 바이트 [] getFileByteArray (문자열적인 filePath) { 바이트 [완충액 = 널 ; 파일 파일 = 새로운 파일 (적인 filePath); FileInputStream의 FIS; 시도 (file.exists ()) {{ 있는 ByteArrayOutputStream BOS = 새 를 ByteArrayOutputStream (); FIS는 = 새로운 FileInputStream에 (파일); 바이트 [] B = 새로운 바이트 [1024 ]; INT의 N; 반면 ((N = fis.read (b)) = -1! ) { bos.write (b, 0 , N); } fis.close (); bos.close (); 버퍼 = bos.toByteArray (); 만약 File.delete를 (); } }캐치 (FileNotFoundException이 전자) { e.printStackTrace (); } 캐치 (IOException이 전자) { e.printStackTrace (); } 리턴 버퍼; } / ** * * @param의 fileName에 * @return * / 공용 정적 부울 isEndsWithZip (문자열 파일명) { 부울 플래그 = 거짓 ; 경우 (파일 이름! = 널 (null) &&! "" 경우.equals (fileName.trim ())) { (fileName.endsWith ( "ZIP.") || fileName.endsWith ( "우편." )) { 플래그 = 진정한 ; } } 반환 플래그를; } }