JAVA自带API的压缩与解压
来源:互联网 发布:minitab软件统计分析 编辑:程序博客网 时间:2024/05/22 05:18
Java API中的 java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。
ZipFile
java中的每一个压缩文件都是可以使用ZipFile来进行表示的。
File file = new File("F:/zippath.zip"); ZipFile zipFile = new ZipFile(file); System.out.println("压缩文件的名称为:" + zipFile.getName());
压缩单个文件
/** 压缩单个文件*/ public static void ZipFile(String filepath ,String zippath) { try { File file = new File(filepath); File zipFile = new File(zippath); InputStream input = new FileInputStream(file); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.putNextEntry(new ZipEntry(file.getName())); int temp = 0; while((temp = input.read()) != -1){ zipOut.write(temp); } input.close(); zipOut.close();} catch (Exception e) {e.printStackTrace();}}
应用:
ZipFile("d:/hello.txt", "d:/hello.zip");
压缩多个文件(文件夹)
/** 一次性压缩多个文件,文件存放至一个文件夹中*/ public static void ZipMultiFile(String filepath ,String zippath) {try { File file = new File(filepath);// 要被压缩的文件夹 File zipFile = new File(zippath); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); if(file.isDirectory()){ File[] files = file.listFiles(); for(int i = 0; i < files.length; ++i){ input = new FileInputStream(files[i]); zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName())); int temp = 0; while((temp = input.read()) != -1){ zipOut.write(temp); } input.close(); } } zipOut.close();} catch (Exception e) {e.printStackTrace();}}
应用:
ZipMultiFile("f:/uu", "f:/zippath.zip");
解压缩单个文件
/** 解压缩(解压缩单个文件)*/ public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {try { File file = new File(zippath);//压缩文件路径和文件名 File outFile = new File(outfilepath);//解压后路径和文件名 ZipFile zipFile = new ZipFile(file); ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名 InputStream input = zipFile.getInputStream(entry); OutputStream output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close();} catch (Exception e) {e.printStackTrace();}}
应用:
ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
解压缩多个文件
ZipInputStream类:
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了。
如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类。
/** 解压缩(压缩文件中包含多个文件)可代替上面的方法使用。 * ZipInputStream类 * 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了, * 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类 * */ public static void ZipContraMultiFile(String zippath ,String outzippath){ try { File file = new File(zippath); File outFile = null; ZipFile zipFile = new ZipFile(file); ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file)); ZipEntry entry = null; InputStream input = null; OutputStream output = null; while((entry = zipInput.getNextEntry()) != null){ System.out.println("解压缩" + entry.getName() + "文件"); outFile = new File(outzippath + File.separator + entry.getName()); if(!outFile.getParentFile().exists()){ outFile.getParentFile().mkdir(); } if(!outFile.exists()){ outFile.createNewFile(); } input = zipFile.getInputStream(entry); output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); }} catch (Exception e) {e.printStackTrace();} }
应用:
ZipContraMultiFile("f:/zippath.zip", "d:/"); ZipContraMultiFile("d:/hello.zip", "d:/");
0 0
- JAVA自带API的压缩与解压
- Java JDK自带的压缩和解压API
- .NET 自带的压缩和解压
- C#xml的压缩与解压还原(使用系统自带的压缩与解压)(源码分享)
- 使用java自带的zip api压缩解压缩文本
- JAVA API 自带图片压缩
- JAVA API实现ZIP压缩与解压
- windows系统自带压缩解压命令
- java 压缩与解压
- java 压缩与解压
- java zip文件的压缩与解压
- Java对字符串的压缩与解压
- Java实现文件的压缩与解压
- Java中文件的压缩与解压
- Java实现文件的压缩与解压
- Java实现文件的压缩与解压
- Java解压和压缩带密码的zip文件
- Java解压和压缩带密码的zip文件
- Spring 构造注入和设置注入
- js autocomplete 搜索提示关键字、选择关键字
- 贪吃蛇视频20%
- Leetcode 之 Intersection of Two Arrays II
- 机器学习中的数学(4)-线性判别分析(LDA), 主成分分析(PCA)
- JAVA自带API的压缩与解压
- STL之map容器使用
- socket编程的同步、异步与阻塞、非阻塞示例详解之二
- 排序算法-堆排序
- OSM数据转ArcGIS可用格式
- [DFT] Spyglass DFT
- python——怎样导入其他地方的python模块
- UVa - 1220 - Party at Hali-Bula ( 树形DP 求最大独立集 )
- line-height的继承性,height line-height 背景填充 的方式