使用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();
}
}
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();
}
}
相关文章推荐
- Java使用7Zip完成文件的压缩和解压缩
- java 使用7-zip解压缩带密码的压缩文件(非Proccess方法)
- java 使用线程监控文件目录变化的实现方法
- Java实现Zip压缩/解压缩目录中的所有文件
- 使用Java实现文件的压缩和解压缩
- 使用java.util.zip压缩、解压文件
- Java源码——使用JFileChooser获取文件及目录信息(Obtain file and directory information)
- Java压缩/解压缩二进制文件--http中content-encoding:deflate也可以使用
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- 使用java的输出流写一个json格式的文件在指定目录下
- 关于使用java自带ZipOutputStream压缩文件名为中文的乱码问题
- Java压缩文件工具类ZipUtil使用方法代码示例
- 使用java代码在本地创建文件夹(多层目录),在本地磁盘创建文件目录
- 文件压缩java使用Apache的ant压缩和解压文件(zip)
- Java基础之IO流,使用File类以树形结构打印指定文件目录
- 实例展示使用Java压缩和解压缩7z文件的方法
- 使用java实现dos窗口文件操作命令(复制,删除,列出文件目录)
- 用Java实现ZIP压缩文件和目录程序代码
- Java中使用Ant压缩指定文件夹或文件
- 使用JSmooth将java程序转换成windows上的.exe文件(彻底解决相对目录问题)