java 압축 및 압축 해제 Zip, Jar, Gzip 파일 실례 코드
다음은 주로 ZIP 형식의 압축 파일이고 JAR, GZIP 형식의 압축 파일도 비슷한 용법이다.
ZIP는 매우 흔히 볼 수 있는 압축 형식으로 자바에서 ZIP의 압축을 실현하려면 주로java를 사용한다.util.zip 이 가방 안의 종류.주로 ZipFile, ZipOutputStream, ZipInputStream, ZipEntry가 있습니다.ZipOutputStream은 파일을 압축하는 데 사용되고, ZipInputStream과 ZipFile은 압축을 풀기 위해 사용되며, 압축과 압축을 풀면서 ZipEntry가 사용됩니다.자바의 Zip 압축 파일에서 모든 하위 파일은 Zip Entry 대상입니다.
압축 파일:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputStreamTest {
 public static void main(String args[]) throws IOException {
 test1();
 test2();
 }
 
 public static void test1() throws IOException {
 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\testZip.zip"), Charset.forName("GBK"));
 // ab.txt ZipEntry 
 ZipEntry entry = new ZipEntry("ab.txt");
 // 
 zos.setComment("zip for ");
 // ZipEntry , ZipEntry 
 zos.putNextEntry(entry);
 InputStream is = new FileInputStream("D:\\ab.txt");
 int len = 0;
 while ((len = is.read()) != -1)
  zos.write(len);
 is.close();
 zos.close();
 }
 
 public static void test2() throws IOException {
 File inFile = new File("D:\\test");
 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\test.zip"), Charset.forName("GBK"));
 zos.setComment(" ");
 zipFile(inFile, zos, "");
 zos.close();
 }
 
 public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException {
 if (inFile.isDirectory()) {
  File[] files = inFile.listFiles();
  for (File file:files)
  zipFile(file, zos, dir + "\\" + inFile.getName());
 } else {
  String entryName = null;
  if (!"".equals(dir))
  entryName = dir + "\\" + inFile.getName();
  else
  entryName = inFile.getName();
  ZipEntry entry = new ZipEntry(entryName);
  zos.putNextEntry(entry);
  InputStream is = new FileInputStream(inFile);
  int len = 0;
  while ((len = is.read()) != -1)
  zos.write(len);
  is.close();
 }
 }
 
}
파일 압축 풀기:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipInputStreamTest {
 public static void main(String args[]) throws IOException {
 File file = new File("D:\\test.zip");// 
 ZipFile zipFile = new ZipFile(file);// ZipFile, zip ZipFile
 // Zip ZipInputStream , getNextEntry() ZipEntry 
 ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
 ZipEntry zipEntry = null;
 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
  String fileName = zipEntry.getName();
  File temp = new File("D:\\unpackTest\\" + fileName);
  if (! temp.getParentFile().exists())
  temp.getParentFile().mkdirs();
  OutputStream os = new FileOutputStream(temp);
  // ZipFile getInputStream ZipEntry 
  InputStream is = zipFile.getInputStream(zipEntry);
  int len = 0;
  while ((len = is.read()) != -1)
  os.write(len);
  os.close();
  is.close();
 }
 zipInputStream.close();
 }
 
}
이상은 Java의 압축 파일 압축 및 해제에 대한 자료 정리입니다. 다음에 관련 자료를 계속 보충합니다. 본 사이트에 대한 지지에 감사드립니다!
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.