需求,对Spring传递上来的文件进行解压,分析数据,这是解压模块
<!--apache提供的压缩包依赖--><dependency><groupId>mons</groupId><artifactId>commons-compress</artifactId><version>1.9</version></dependency><!--不引入xz依赖会在new SevenZFile的时候报错java.lang.NoClassDefFoundError: org/tukaani/xz/FilterOptions--><dependency><groupId>org.tukaani</groupId><artifactId>xz</artifactId><version>1.9</version></dependency><!--解压rar5,所需依赖开始--><dependency><groupId>com.github.axet</groupId><artifactId>java-unrar</artifactId><version>1.7.0-8</version></dependency><dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding</artifactId><version>16.02-2.01</version></dependency><dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding-all-platforms</artifactId><version>16.02-2.01</version></dependency>
1、解压RaR
@MethodOperation("解压rar 5版本向下兼容")public static void unRar(InputStream inputStream, String outPath, String fileName) throws IOException {File file = null;IInArchive archive = null;RandomAccessFile randomAccessFile = null;try {//将文件流放到一个临时位置后转换为File对象String filePath = outPath + File.separator + fileName;new File(outPath).mkdirs();file = new File(filePath);log.info("创建的文件路径:{}", filePath);inputStream2File(inputStream, file);randomAccessFile = new RandomAccessFile(file, "r");archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));int[] in = new int[archive.getNumberOfItems()];for (int i = 0; i < in.length; i++) {in[i] = i;}archive.extract(in, false, new ExtractCallback(archive, outPath));} catch (Exception e) {e.printStackTrace();} finally {try {if (null != archive) {archive.close();}if (null != randomAccessFile) {randomAccessFile.close();}file.deleteOnExit();file.delete();} catch (Exception e) {e.printStackTrace();}log.info("RAR文件解压成功");}}
ExtractCallback,引用《/cxy_zxl/article/details/121508705》
public class ExtractCallback implements IArchiveExtractCallback {private int index;private IInArchive inArchive;private String ourDir;public ExtractCallback(IInArchive inArchive, String ourDir) {this.inArchive = inArchive;this.ourDir = ourDir;}@Overridepublic void setCompleted(long arg0) throws SevenZipException {}@Overridepublic void setTotal(long arg0) throws SevenZipException {}@Overridepublic ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {this.index = index;final String path = (String) inArchive.getProperty(index, PropID.PATH);final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);final String[] oldPath = {""};return new ISequentialOutStream() {public int write(byte[] data) throws SevenZipException {try {if (!isFolder) {File file = new File(ourDir + File.separator + path);if (path.equals(oldPath[0])) {save2File(file, data, true);} else {save2File(file, data, false);}oldPath[0] = path;}} catch (Exception e) {e.printStackTrace();}return data.length;}};}@Overridepublic void prepareOperation(ExtractAskMode arg0) throws SevenZipException {}@Overridepublic void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {}/*** 解决字节丢失 未验证** @param file* @param msg* @param append* @return*/public boolean save2File(File file, byte[] msg, boolean append) {OutputStream fos = null;try {File parent = file.getParentFile();boolean bool;if ((!parent.exists()) && (!parent.mkdirs())) {return false;}//是否追加fos = new FileOutputStream(file, append);fos.write(msg);fos.flush();return true;} catch (FileNotFoundException e) {return false;} catch (IOException e) {File parent;return false;} finally {if (fos != null) {try {fos.close();} catch (IOException e) {}}}}public static boolean save2File(File file, byte[] msg) {OutputStream fos = null;try {File parent = file.getParentFile();if ((!parent.exists()) && (!parent.mkdirs())) {return false;}fos = new FileOutputStream(file, true);fos.write(msg);fos.flush();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;} finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
2、解压7Z
@MethodOperation("解析本地7z文件")public static void un7z(InputStream inputStream, String outPath, String fileName) {SevenZFile sevenZFile = null;File tempfile = null;try {//将文件流放到一个临时位置后转换为File对象new File(outPath).mkdirs();tempfile = new File(outPath + File.separator + fileName);inputStream2File(inputStream, tempfile);//获取对象sevenZFile = new SevenZFile(tempfile);SevenZArchiveEntry nextEntry = null;while ((nextEntry = sevenZFile.getNextEntry()) != null) {File file = new File(outPath + File.separator + nextEntry.getName());if (nextEntry.isDirectory()) {file.mkdir();continue;}fileOutput(file, sevenZFile, nextEntry);}} catch (IOException e) {e.printStackTrace();} finally {try {closeInputStream(inputStream);if (null != sevenZFile) {sevenZFile.close();}//删除临时文件tempfile.deleteOnExit();tempfile.delete();} catch (Exception e) {}log.info("7Z文件解压成功");}}
3、解压ZIP
@MethodOperation("解析本地unZip文件")public static void unZip(InputStream inputStream, String outPath) throws IOException {ZipInputStream zipInputStream = null;try {zipInputStream = new ZipInputStream(inputStream, Charset.forName("gbk"));ZipEntry nextEntry = zipInputStream.getNextEntry();while (nextEntry != null) {File file = new File(outPath + File.separator + nextEntry.getName());if (nextEntry.isDirectory()) {file.mkdir();nextEntry = zipInputStream.getNextEntry();nextEntry.clone();continue;}fileOutput(zipInputStream, file);nextEntry.clone();nextEntry = zipInputStream.getNextEntry();}} catch (Exception e) {e.printStackTrace();} finally {try {zipInputStream.closeEntry();closeInputStream(inputStream);if (null != zipInputStream) {zipInputStream.close();}} catch (Exception e) {e.printStackTrace();}log.info("ZIP文件解压成功");}}
4、其他分解的方法
@MethodOperation("将文件输出到某个位置")private static void fileOutput(InputStream inputStream, File file) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream(file);BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);int n;byte[] bytes = new byte[1024];while ((n = inputStream.read(bytes)) != -1) {bufferedOutputStream.write(bytes, 0, n);}bufferedOutputStream.close();fileOutputStream.close();}@MethodOperation("文件输出")private static void fileOutput(File file, SevenZFile sevenZFile, SevenZArchiveEntry entry) throws IOException {FileOutputStream out = null;try {out = new FileOutputStream(file);byte[] content = new byte[(int) entry.getSize()];sevenZFile.read(content, 0, content.length);out.write(content);} catch (Exception e) {e.printStackTrace();} finally {try {if (out != null) {out.close();}} catch (Exception e) {e.printStackTrace();}}}@MethodOperation("将inputStream转化为file")private static void inputStream2File(InputStream is, File file) throws IOException {OutputStream os = null;try {os = new FileOutputStream(file);int len = 0;byte[] buffer = new byte[8192];while ((len = is.read(buffer)) != -1) {os.write(buffer, 0, len);}} finally {try {is.close();os.close();} catch (Exception e) {e.printStackTrace();}}}@MethodOperation("关闭流")private static void closeInputStream(InputStream inputStream) {try {if (inputStream != null) {inputStream.close();}} catch (Exception e) {}}
如果觉得《java对压缩文件7z rar zip的解压》对你有帮助,请点赞、收藏,并留下你的观点哦!