파일 압축 풀기 도구 클래스 ZipUtil
package com.imooc.test;
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.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
*
* @version 1.0
* @since 2015-9-14
*
*
*/
public final class ZipUtil
{
/**
*
*/
private static int BUFFERSIZE = 2 << 10;
/**
*
* @param paths
* @param fileName
*/
public static void zip(String[] paths, String fileName)
{
ZipOutputStream zos = null;
try
{
zos = new ZipOutputStream(new FileOutputStream(fileName));
for(String filePath : paths)
{
//
File file = new File(filePath);
String relativePath = file.getName();
if(file.isDirectory())
{
relativePath += File.separator;
}
zipFile(file, relativePath, zos);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(zos != null)
{
zos.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void zipFile(File file, String relativePath, ZipOutputStream zos)
{
InputStream is = null;
try
{
if(!file.isDirectory())
{
ZipEntry zp = new ZipEntry(relativePath);
zos.putNextEntry(zp);
is = new FileInputStream(file);
byte[] buffer = new byte[BUFFERSIZE];
int length = 0;
while ((length = is.read(buffer)) >= 0)
{
zos.write(buffer, 0, length);
}
zos.flush();
zos.closeEntry();
}
else
{
for(File f: file.listFiles())
{
zipFile(f, relativePath + f.getName() + File.separator, zos);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(is != null)
{
is.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
*
* @param fileName
* @param path
*/
public static void unzip(String fileName, String path)
{
FileOutputStream fos = null;
InputStream is = null;
try
{
ZipFile zf = new ZipFile(new File(fileName));
Enumeration en = zf.entries();
while (en.hasMoreElements())
{
ZipEntry zn = (ZipEntry) en.nextElement();
if (!zn.isDirectory())
{
is = zf.getInputStream(zn);
File f = new File(path + zn.getName());
File file = f.getParentFile();
file.mkdirs();
fos = new FileOutputStream(path + zn.getName());
int len = 0;
byte bufer[] = new byte[BUFFERSIZE];
while (-1 != (len = is.read(bufer)))
{
fos.write(bufer, 0, len);
}
fos.close();
}
}
}
catch (ZipException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(null != is)
{
is.close();
}
if(null != fos)
{
fos.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
zip(new String[] {"D:/tmp/20150418/logs/file/2015-08-28/debug-log.log","D:/tmp/20150418/logs/file/2015-08-28/error-log.log"}, "D:/tmp/20150418/logs/file/2015-08-28/test.zip");
unzip("D:/tmp/20150418/logs/file/2015-08-28/test.zip", "D:/test/");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.