//压缩文件
//先用输入流读取文件,之后再用压缩流输出文件
public static void main(String[] args) throws Exception {
File file = new File("d:" + File.separator + "demo.pdf");// 源文件
File zipFile = new File("d:" + file.separator + "1.zip");// 被压缩文件的路径
InputStream fileReader = new FileInputStream(file);// 需要读取源文件
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
// 每一个压缩文件的子文件都用ZipEntry表示
// 需要为每一个压缩的文件,设置名称
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
zipOutputStream.setComment("这是一个压缩的pdf");// 压缩文件的注释
int temp = 0;
while ((temp = fileReader.read()) != -1) {// 读取内容
zipOutputStream.write(temp);// 压缩输出内容
}
fileReader.close();//关闭输入流
zipOutputStream.close();//关闭压缩流
}
Java.io.压缩文件
最新推荐文章于 2024-10-12 17:55:45 发布