자바 파일 압축 및 압축 풀기 (2)
package com.cn;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
// :
//1 file.isFile() file.isDirectory() .
// Directory file,file Directory
// :file Directory . file Directory .
// 1 .
//2 File f=new File("F:\\x.txt");
// x.txt .
// .
// File f=new File("F:\\x.txt");
// if (!f.exists()) {
// f.createNewFile();
// }
// f.createNewFile()
//
// , File f=new File("F:\\x.txt")
// , f , x.txt
// hello world
//
//3 :
// zip() zos.
// :
//1 zip unzip !!!
// directory, , .
// , directory, .
//2 JAVA ZipEntry
// ( )
// new() ZipEntry
//3 zipEntry .
// entrys.hasMoreElements()
// zipEntry.
// :
// new File(unzipPath+File.separator+entry.getName());
public class TestZipAndUnZip {
public static void main(String[] args) throws Exception {
TestZipAndUnZip test=new TestZipAndUnZip();
// zip
test.zip("F:\\", "kk\\cc.txt", "F:\\cc1.zip");
// zip
test.unZipFile("F:\\cc1.zip", "F:\\zzzz");
}
/**
* @param willZipDirPath ( )
* @param willZipFileName ( )
* @param toFilePath ( )
*/
public void zip(String willZipDirPath, String willZipFileName, String zipedFileName) {
System.out.println("………………… zip() …………………………");
if (willZipDirPath == null) {
return;
}
File willZipDir = new File(willZipDirPath);
if (!willZipDir.exists() || !willZipDir.isDirectory()) {
return;
}
//
String willZipDirAbsolutePath = willZipDir.getAbsolutePath();
System.out.println("willZipDir.getAbsolutePath()="+willZipDirAbsolutePath);
//
File zipedFile = new File(zipedFileName);
try {
// ZipOutputStream
// zos .
// ?
// ZipEntry!!!
// fileToZip() ZipEntry !!
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipedFile));
if (willZipFileName.equals("*")) {
// *
// dirToZip()
dirToZip(willZipDirAbsolutePath, willZipDir, zos);
} else {
//
File willZipFile = new File(willZipDirPath, willZipFileName);
if (willZipFile.isFile()) {
System.out.println("………………… ………………………");
fileToZip(willZipDirPath, willZipFile, zos);
System.out.println("………………… ………………………");
}
if (willZipFile.isDirectory()) {
System.out.println("………………… ………………………");
dirToZip(willZipDirPath, willZipFile, zos);
System.out.println("………………… ………………………");
}
// !!!
zos.close();
System.out.println("………………… zip() …………………………");
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* @param dirPath
* @param willZipFile
* @param zos
*/
public void fileToZip(String dirPath, File willZipFile,ZipOutputStream zos){
FileInputStream fis=null;
ZipEntry zipEntry=null;
byte [] buffer=new byte[1024*8];
int len=0;
if (willZipFile.isFile()) {
try {
fis=new FileInputStream(willZipFile);
zipEntry=new ZipEntry(getEntryName(dirPath, willZipFile));
zos.putNextEntry(zipEntry);
System.out.println("………………… fileToZip() …………………………");
System.out.println("zipEntry.getName="+zipEntry.getName());
System.out.println("zipEntry.isDirectory="+zipEntry.isDirectory());
System.out.println("zipEntry.getSize="+zipEntry.getSize());
System.out.println("zipEntry.getTime="+zipEntry.getTime());
System.out.println("zipEntry.getComment="+zipEntry.getComment());
System.out.println("………………… fileToZip() …………………………");
while((len=fis.read(buffer))!=-1){
zos.write(buffer, 0, len);
}
zos.closeEntry();
fis.close();
} catch (Exception e) {
}
}
}
/**
* @param dirPath
* @param willZipDir
* @param zos
*/
public void dirToZip(String dirPath, File willZipDir, ZipOutputStream zos) {
if (willZipDir.isDirectory()) {
File[] files = willZipDir.listFiles();
// -->
if (files.length==0) {
ZipEntry zipEntry=new ZipEntry(getEntryName(dirPath, willZipDir));
System.out.println("xxxxxxxxxxxxxxxx "+zipEntry.getName());
try {
zos.putNextEntry(zipEntry);
//zos.closeEntry();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
// -->
for (int i = 0; i < files.length; i++) {
File file = files[i];
// , fileToZip()
if (file.isFile()) {
System.out.println("xxxxxxxxxx fileToZip() xxxxxxxxxx");
fileToZip(dirPath, file, zos);
System.out.println("xxxxxxxxxx fileToZip() xxxxxxxxxx");
}
// , dirToZip()
if (file.isDirectory()) {
System.out.println("xxxxxxxxxx dirToZip() xxxxxxxxxx");
dirToZip(dirPath, file, zos);
System.out.println("xxxxxxxxxx dirToZip() xxxxxxxxxx");
}
}
}
}
/**
* @param dirPath
* @param willZipFile
* @return
*/
// ( )
// zipEntry .
public String getEntryName(String dirPath, File willZipFile) {
if (!dirPath.endsWith(File.separator)) {
dirPath += File.separator;
}
String willZipFilePath=willZipFile.getAbsolutePath();
if (willZipFile.isDirectory()) {
willZipFilePath+="/";
}
int index=willZipFilePath.indexOf(dirPath);
System.out.println("xx entryName="+ willZipFilePath.substring(index+dirPath.length()));
return willZipFilePath.substring(index+dirPath.length());
}
/**
* @param zipedFileName zip
* @param unzipDirPath
* @throws IOException
*/
public void unZipFile(String zipedFileName,String unzipDirPath) throws Exception{
if (!unzipDirPath.endsWith(File.separator)) {
unzipDirPath+=File.separator;
}
try {
ZipFile zipedFile=new ZipFile(zipedFileName);
ZipEntry zipEntry=null;
String entryName=null;
String unzipedFileName=null;
Enumeration entrys=zipedFile.entries();
byte [] buffer=new byte[1024*8];
int len=0;
while (entrys.hasMoreElements()) {
zipEntry=(ZipEntry) entrys.nextElement();
entryName=zipEntry.getName();
unzipedFileName=unzipDirPath+entryName;
System.out.println("………………… unZipFile() …………………………");
System.out.println("zipedFileName="+zipedFileName);
System.out.println("unzipDirPath="+unzipDirPath);
System.out.println("entryName="+entryName);
System.out.println("unzipedFileName="+unzipedFileName);
System.out.println("………………… unZipFile() …………………………");
if (zipEntry.isDirectory()) {
//
System.out.println("999999999999");
new File(unzipedFileName).mkdirs();
} else {
// . .
new File(unzipedFileName).getParentFile().mkdirs();
}
FileOutputStream fos=null;
InputStream is=null;
File unzipedFile=new File(unzipedFileName);
if (unzipedFile.isDirectory()) {
File [] files=unzipedFile.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
fos=new FileOutputStream(file);
is=zipedFile.getInputStream(zipEntry);
while ((len=is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
}
}else{
fos=new FileOutputStream(unzipedFile);
is=zipedFile.getInputStream(zipEntry);
while ((len=is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
}
//
//fos.close();
//is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.