编辑
2023-04-12
学习记录
00
请注意,本文编写于 738 天前,最后修改于 737 天前,其中某些信息可能已经过时。

目录

简介
读取
下载文件并解密
清除zip目录

简介

先读取压缩文件到本地,然后解压,再向前端输出文件夹中的内容

读取

这部分忽略,可以直接查看之前的文章,直接从ftpUtil获取文件,得到ByteArrayOutputStream开始,讲解

下载文件并解密

java
//通过zip获取pdf public ByteArrayOutputStream getPdfBase64ByZip(ByteArrayOutputStream fileData,String fileName) throws Exception { ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData.toByteArray()); String base64= Base64Encoder.encode(IOUtils.toByteArray(inputStream)); System.out.println(base64); String unzipPath=RecMrDetailIndexController.class.getResource("/unzip/").getPath(); String zipName=unzipPath+fileName.split("as")[0]+"zip"; FileUtils.base64ToFile(zipName,base64); ZipUtil.unZip(zipName,unzipPath,"mars"); String pdfName=unzipPath+fileName.split("as")[0]+"pdf"; System.out.println("pdfName"+pdfName); File file=new File(pdfName); FileInputStream fileInputStream=new FileInputStream(file); byte[] bytes=IOUtils.toByteArray(fileInputStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length); outputStream.write(bytes, 0, bytes.length); return outputStream; }

将ByteArrayOutputStream 下载到unzip文件夹,base64转文件并且解压缩,获取压缩包内文件并且返回。

base64转文件方法

java
public static void base64ToFile(String targetFile,String base64 ) { File file = null; File dir=new File("d:\\"); if (!dir.exists() && !dir.isDirectory()) { dir.mkdirs(); } BufferedOutputStream bos = null; java.io.FileOutputStream fos = null; try { byte[] bytes = Base64.getDecoder().decode(base64); file=new File(targetFile); fos = new java.io.FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }

解压加密zip方法,zipUtil

java
/** * @param source * 原始文件路径 * @param dest * 解压路径 * @param password * 解压文件密码(可以为空) */ public static void unZip(String source, String dest, String password) { try { File zipFile = new File(source); // 首先创建ZipFile指向磁盘上的.zip文件 ZipFile zFile = new ZipFile(zipFile); zFile.setFileNameCharset("GBK"); File destDir = new File(dest); if (!destDir.exists()) { destDir.mkdirs(); } if (zFile.isEncrypted()) { // 设置密码 zFile.setPassword(password.toCharArray()); } // 将文件抽出到解压目录(解压) zFile.extractAll(dest); List<FileHeader> headerList = zFile.getFileHeaders(); List<File> extractedFileList = new ArrayList<File>(); for (FileHeader fileHeader : headerList) { if (!fileHeader.isDirectory()) { extractedFileList.add(new File(destDir, fileHeader.getFileName())); } } File[] extractedFiles = new File[extractedFileList.size()]; extractedFileList.toArray(extractedFiles); for (File f : extractedFileList) { System.out.println(f.getAbsolutePath() + "文件解压成功!"); } } catch (ZipException e) { e.printStackTrace(); } }

清除zip目录

java
//删除临时路径下的文件 public boolean clearUnZipDir() throws IOException { String unzipPath= null; try { unzipPath = RecMrDetailIndexController.class.getResource("/unzip/").toURI().getPath(); } catch (URISyntaxException e) { e.printStackTrace(); } if (StrUtil.isBlank(unzipPath)){ return false; } File dirZip = new File(unzipPath); if (dirZip.isDirectory()) { // 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错 org.apache.commons.io.FileUtils.cleanDirectory(dirZip); } return true; }

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!