java 压缩代码_Java实现多文件压缩和解压缩代码

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

Java实现多文件压缩和解压缩代码

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

public class TestZIP {

/**

* 功能:压缩多个文件成一个zip文件

* @param srcfile:源文件列表

* @param zipfile:压缩后的文件

*/

public static void zipFiles(File[] srcfile,File zipfile) {

byte[] buf = new byte[1024];

try {

//ZipOutputStream类:完成文件或文件夹的压缩

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

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

FileInputStream in = new FileInputStream(srcfile[i]);

out.putNextEntry(new ZipEntry(srcfile[i].getName()));

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf,len);

}

out.closeEntry();

in.close();

}

out.close();

System.out.println("压缩完成.");

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 功能:解压缩

* @param zipfile:需要解压缩的文件

* @param descDir:解压后的目标目录

*/

public static void unZipFiles(File zipfile,String descDir) {

try {

ZipFile zf = new ZipFile(zipfile);

for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {

ZipEntry entry = (ZipEntry) entries.nextElement();

String zipEntryName = entry.getName();

InputStream in = zf.getInputStream(entry);

OutputStream out = new FileOutputStream(descDir + zipEntryName);

byte[] buf1 = new byte[1024];

int len;

while ((len = in.read(buf1)) > 0) {

out.write(buf1,len);

}

in.close();

out.close();

System.out.println("解压缩完成.");

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**功能:

* @param args

*/

public static void main(String[] args) {

//2个源文件

File f1 = new File("D:\\test\\1.csv");

File f2 = new File("D:\\test\\2.xlsx");

File[] srcfile = { f1,f2 };

//压缩后的文件

File zipfile = new File("D:\\test\\3.zip");

TestZIP.zipFiles(srcfile,zipfile);

//需要解压缩的文件

File file = new File("D:\\test\\3.zip");

//解压后的目标目录

String dir = "D:\\test\\1\\";

TestZIP.unZipFiles(file,dir);

}

} 转自:http://blog.csdn.net/zdp072/article/details/44177925

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以通过使用ZipOutputStream和ZipInputStream类来实现文件压缩解压缩,但是这两个类只支持Zip格式的压缩文件。对于PDF格式的文件,可以使用iText库来实现压缩解压缩。 以下是一个使用iText库实现PDF文件压缩解压缩示例代码: ```java import java.io.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; public class PDFCompressor { public static void compressPdf(String srcFile, String destFile) throws Exception { PdfReader reader = new PdfReader(srcFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile)); PdfCompressionSettings compression = new PdfCompressionSettings(); compression.setCompressionLevel(PdfStream.BEST_COMPRESSION); stamper.setCompressionLevel(9); stamper.setFullCompression(); stamper.getWriter().setCompressionLevel(9); stamper.getWriter().setCompressionLevel(9, compression); stamper.close(); reader.close(); } public static void decompressPdf(String srcFile, String destFile) throws Exception { PdfReader.unethicalreading = true; PdfReader reader = new PdfReader(srcFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile)); stamper.getWriter().setCompressionLevel(0); stamper.close(); reader.close(); } public static void main(String[] args) { try { compressPdf("example.pdf", "example_compressed.pdf"); decompressPdf("example_compressed.pdf", "example_decompressed.pdf"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,compressPdf方法用于压缩PDF文件,decompressPdf方法用于解压缩PDF文件。在compressPdf方法中,我们使用PdfStamper类来设置PDF文件压缩级别和压缩方式。在decompressPdf方法中,我们将PDF文件压缩级别设置为0,以实现解压缩。 需要注意的是,iText库需要额外下载并添加到项目中。可以从官网https://itextpdf.com/zh/download下载最新版本的iText库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值