- 2023-01-09 15:22:09
- 1340 热度
- 0 评论
传入一组文件,将文件列表打包为ZIP文件,传入ZIP文件将文件解压缩到一个指定文件夹,下面是这个实例的实现
使用到了ANT的工具类,所以要引入ant.jar文件到工程中
对于压缩建立一个工具类:
package com.xidian.commmon; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.zip.ZipOutputStream; import org.apache.tools.zip.ZipEntry; /** * ZIP操作工具类 * @说明 注意引入ant.jar文件 * @author cuisuqiang * @version 1.0 * @since */ public class Zip { /** * 压缩文件列表到某ZIP文件 * @param zipFilename 要压缩到的ZIP文件 * @param paths 文件列表,多参数 * @throws Exception */ public static void compress(String zipFilename, String... paths) throws Exception { compress(new FileOutputStream(zipFilename), paths); } /** * 压缩文件列表到输出流 * @param os 要压缩到的流 * @param paths 文件列表,多参数 * @throws Exception */ public static void compress(OutputStream os, String... paths) throws Exception { ZipOutputStream zos = new ZipOutputStream(os); for (String path : paths) { if (path.equals("")) continue; java.io.File file = new java.io.File(path); if (file.exists()) { if (file.isDirectory()) { zipDirectory(zos, file.getPath(), file.getName() + File.separator); } else { zipFile(zos, file.getPath(), ""); } } } zos.close(); } private static void zipDirectory(ZipOutputStream zos, String dirName, String basePath) throws Exception { File dir = new File(dirName); if (dir.exists()) { File files[] = dir.listFiles(); if (files.length > 0) { for (File file : files) { if (file.isDirectory()) { zipDirectory(zos, file.getPath(), basePath + file.getName().substring( file.getName().lastIndexOf( File.separator) + 1) + File.separator); } else zipFile(zos, file.getPath(), basePath); } } else { ZipEntry ze = new ZipEntry(basePath); zos.putNextEntry(ze); } } } private static void zipFile(ZipOutputStream zos, String filename, String basePath) throws Exception { File file = new File(filename); if (file.exists()) { FileInputStream fis = new FileInputStream(filename); ZipEntry ze = new ZipEntry(basePath + file.getName()); zos.putNextEntry(ze); byte[] buffer = new byte[8192]; int count = 0; while ((count = fis.read(buffer)) > 0) { zos.write(buffer, 0, count); } fis.close(); } } }
String... paths 是传入的文件列表,输出流可以穿文件流也可以传文件路径名称
压缩一组文件:
package com.xidian.test; import java.io.FileOutputStream; import com.xidian.commmon.Zip; /** * 压缩文件 * @说明 * @author cuisuqiang * @version 1.0 * @since */ public class TestCompress { public static void main(String[] args) { // 要压缩的文件列表 String path01 = "C:\\a"; String path02 = "C:\\test.txt"; try { FileOutputStream os = new FileOutputStream("C:\\a.zip"); // 输出的ZIP文件流 Zip.compress(os, path01, path02); } catch (Exception e) { e.printStackTrace(); } } }
由于已经写了工具类,调用看起来非常清晰
解压这个压缩包:
package com.xidian.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * 解压缩测试 * @说明 * @author cuisuqiang * @version 1.0 * @since */ public class EctractZip { @SuppressWarnings( { "unchecked", "static-access" }) public static void main(String[] args) { EctractZip z = new EctractZip(); ArrayList<String> a = z.Ectract("C:\\a.zip", "C:\\tmp\\"); // 返回解压缩出来的文件列表 for(String s : a){ System.out.println(s); } } /** * 解压缩 * @param sZipPathFile 要解压的文件 * @param sDestPath 解压到某文件夹 * @return */ @SuppressWarnings("unchecked") public static ArrayList Ectract(String sZipPathFile, String sDestPath) { ArrayList<String> allFileName = new ArrayList<String>(); try { // 先指定压缩档的位置和档名,建立FileInputStream对象 FileInputStream fins = new FileInputStream(sZipPathFile); // 将fins传入ZipInputStream中 ZipInputStream zins = new ZipInputStream(fins); ZipEntry ze = null; byte[] ch = new byte[256]; while ((ze = zins.getNextEntry()) != null) { File zfile = new File(sDestPath + ze.getName()); File fpath = new File(zfile.getParentFile().getPath()); if (ze.isDirectory()) { if (!zfile.exists()) zfile.mkdirs(); zins.closeEntry(); } else { if (!fpath.exists()) fpath.mkdirs(); FileOutputStream fouts = new FileOutputStream(zfile); int i; allFileName.add(zfile.getAbsolutePath()); while ((i = zins.read(ch)) != -1) fouts.write(ch, 0, i); zins.closeEntry(); fouts.close(); } } fins.close(); zins.close(); } catch (Exception e) { System.err.println("Extract error:" + e.getMessage()); } return allFileName; } }
解压其实非常简单,因为JDK 中有一个ZipInputStream对象,只需要使用他为我们服务即可!
代码仅供参考,请多指点。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- MVC(22)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- Web(21)
- CLI(20)
- SpringMVC(19)
- Alibaba(19)
- SpringBoot(17)
- Docker(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- stream(11)
- int(11)
- Myeclipse(11)
- Bug(10)
- not(10)
- ast(9)
- maven(9)
- Map(9)
- Hystrix(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)