Java에서 zip 파일을 압축 및 압축 해제하는 방법
코드:
public class ZipUtil { /** * 压缩文件/文件夹 * */ public static void compress(String srcFilePath, String destFilePath) { File src = new File(srcFilePath); if (!src.exists()) { throw new RuntimeException(srcFilePath + "不存在"); } File zipFile = new File(destFilePath); try { FileOutputStream fos = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); String baseDir = ""; compressbyType(src, zos, baseDir); zos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void compressbyType(File src, ZipOutputStream zos, String baseDir) { if (!src.exists()) return; System.out.println("压缩" + baseDir + src.getName()); if (src.isFile()) { compressFile(src, zos, baseDir); } else if (src.isDirectory()) { compressDir(src, zos, baseDir); } } /** * 压缩文件 */ private static void compressFile(File file, ZipOutputStream zos, String baseDir) { if (!file.exists()) return; try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(baseDir + file.getName()); zos.putNextEntry(entry); int count; byte[] buf = new byte[8019]; while ((count = bis.read(buf)) != -1) { zos.write(buf, 0, count); } bis.close(); } catch (Exception e) { // TODO: handle exception } } /** * 压缩文件夹 * */ private static void compressDir(File dir, ZipOutputStream zos, String baseDir) { if (!dir.exists()) return; File[] files = dir.listFiles(); if(files.length == 0){ try { zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator)); } catch (IOException e) { e.printStackTrace(); } } for (File file : files) { compressbyType(file, zos, baseDir + dir.getName() + File.separator); } } /** * 解压文件/文件夹 */ public static void decompress(String srcPath, String dest) throws Exception { File file = new File(srcPath); if (!file.exists()) { throw new RuntimeException(srcPath + "所指文件不存在"); } ZipFile zf = new ZipFile(file); Enumeration entries = zf.entries(); ZipEntry entry = null; while (entries.hasMoreElements()) { entry = (ZipEntry) entries.nextElement(); System.out.println("解压" + entry.getName()); if (entry.isDirectory()) { String dirPath = dest + File.separator + entry.getName(); File dir = new File(dirPath); dir.mkdirs(); } else { // 表示文件 File f = new File(dest + File.separator + entry.getName()); if (!f.exists()) { //String dirs = FileUtils.getParentPath(f); String dirs = f.getParent(); File parentDir = new File(dirs); parentDir.mkdirs(); } f.createNewFile(); // 将压缩文件内容写入到这个文件中 InputStream is = zf.getInputStream(entry); FileOutputStream fos = new FileOutputStream(f); int count; byte[] buf = new byte[8192]; while ((count = is.read(buf)) != -1) { fos.write(buf, 0, count); } is.close(); fos.close(); } } } public static void main(String[] args) throws Exception { // compress("/Users/liyinlong/lyl/test","/Users/liyinlong/lyl/test.zip"); decompress("/Users/liyinlong/lyl/test.zip", "/Users/liyinlong/lyl/test"); } }
위 내용은 Java에서 zip 파일을 압축 및 압축 해제하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











자바의 암스트롱 번호 안내 여기에서는 일부 코드와 함께 Java의 Armstrong 번호에 대한 소개를 논의합니다.

Java의 난수 생성기 안내. 여기서는 예제를 통해 Java의 함수와 예제를 통해 두 가지 다른 생성기에 대해 설명합니다.

Java의 Weka 가이드. 여기에서는 소개, weka java 사용 방법, 플랫폼 유형 및 장점을 예제와 함께 설명합니다.

Java의 Smith Number 가이드. 여기서는 정의, Java에서 스미스 번호를 확인하는 방법에 대해 논의합니다. 코드 구현의 예.

이 기사에서는 가장 많이 묻는 Java Spring 면접 질문과 자세한 답변을 보관했습니다. 그래야 면접에 합격할 수 있습니다.

Java 8은 스트림 API를 소개하여 데이터 컬렉션을 처리하는 강력하고 표현적인 방법을 제공합니다. 그러나 스트림을 사용할 때 일반적인 질문은 다음과 같은 것입니다. 기존 루프는 조기 중단 또는 반환을 허용하지만 스트림의 Foreach 메소드는이 방법을 직접 지원하지 않습니다. 이 기사는 이유를 설명하고 스트림 처리 시스템에서 조기 종료를 구현하기위한 대체 방법을 탐색합니다. 추가 읽기 : Java Stream API 개선 스트림 foreach를 이해하십시오 Foreach 메소드는 스트림의 각 요소에서 하나의 작업을 수행하는 터미널 작동입니다. 디자인 의도입니다
