您的位置:首页 > 编程语言 > Java开发

使用Java压缩文件及目录

2011-06-03 15:18 351 查看
1、压缩文件



logger.info("Start to compress!");

File file = new File("C:/1.csv");

String zipFileName = "C:/test.zip";

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));

out.setMethod(ZipOutputStream.DEFLATED);

out.putNextEntry(new ZipEntry("1.csv"));

FileInputStream in = new FileInputStream(file);

int b;

while ((b = in.read()) != -1) {

out.write(b);

}

in.close();

out.close();

logger.info("End compressing!");





2、压缩目录

public class TestZip {

public static String SERPEROT = "/";

public static int BUFFER = 2048;



public static void main(String args[]) {

zip("e:/hello/", "e:/hello.zip");

}



public static void zip(String srcFile, String descFile) {

ZipOutputStream zos = null;

FileOutputStream fos = null;

File file = null;

try {

fos = new FileOutputStream(descFile);

zos = new ZipOutputStream(fos);

file = new File(srcFile);

String folder = srcFile.substring(srcFile.lastIndexOf("/") + 1, srcFile.length());

zip(zos, file, folder);

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

try {

if (zos != null) {

zos.close();

}

if (fos != null) {

fos.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

private static void zip(ZipOutputStream descFile, File srcFile, String srcfolder) {

FileInputStream fis = null;

System.out.println(srcFile.isDirectory());

try {

if (srcFile.isDirectory()) {

File[] files = srcFile.listFiles();

descFile.putNextEntry(new ZipEntry(srcfolder + "/")); // 是压缩包里面的路径.

srcfolder = srcfolder.length() == 0 ? "" : srcfolder + "/";

System.out.println(srcfolder);

for (int i = 0; i < files.length; i++) {

zip(descFile, files[i], srcfolder + files[i].getName());

}

} else {

descFile.putNextEntry(new ZipEntry(srcfolder));

fis = new FileInputStream(srcFile);

byte[] bytes = new byte[2048];

int n = 0;

while ((n = fis.read(bytes)) != -1) {

descFile.write(bytes, 0, n);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (fis != null) {

fis.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}



3. 解压目录及文件

解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码

privatevoid
unzipFile(InputStream in)throws IOException {
finalint
bufferSize = 1024;
ZipInputStream zip =
null;
BufferedOutputStream bos =
null;
try {
CheckedInputStream cis =new CheckedInputStream(in,new
CRC32());
zip = new ZipInputStream(cis);
ZipEntry entry =
null;
while ((entry = zip.getNextEntry()) !=null)
{
StringBuilder entryPath =new StringBuilder(PathConstants.SALT_FILE_ROOT);
entryPath.append(entry.getName());
File entryFile =
new File(entryPath.toString());
fileProber(entryFile);
if (entry.isDirectory()) {
entryFile.mkdir();
} else {
bos = new BufferedOutputStream(new
FileOutputStream(entryFile));
int count;
byte data[] =newbyte[bufferSize];
while ((count = zip.read(data, 0, bufferSize))
!= -1) {
bos.write(data, 0, count);
}
bos.close();
}
zip.closeEntry();
}
} finally {
if (zip !=null)
{
try {
zip.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
if (bos !=null)
{
try {
bos.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}

privatevoid
fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
parentFile.mkdir();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: