200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > zip rar解压文件

zip rar解压文件

时间:2020-12-06 08:26:16

相关推荐

zip rar解压文件

1.添加zip、rar依赖

<!-- 解压rar --><dependency><groupId>com.github.junrar</groupId><artifactId>junrar</artifactId><version>3.0.0</version></dependency><!-- 解压zip--><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.10.5</version></dependency>

2.代码

package mon.utils.poi;import java.io.*;import java.util.Enumeration;import mon.utils.file.FileUploadUtils;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;import com.github.junrar.Archive;import com.github.junrar.rarfile.FileHeader;import org.springframework.web.multipart.MultipartFile;/*** 解析zip,rar压缩文件* @author: cp* @date: -02-23 10:21*/public class UnZipOrRarUtil {/** 这里用到了synchronized ,也就是防止出现并发问题 **/public static synchronized void untar(String tarFileName, String extPlace)throws Exception {unRarFile(tarFileName, extPlace);}public static synchronized void unzip(String zipFileName, String extPlace)throws Exception {unZipFiles(zipFileName, extPlace);}/*** 处理文件路径调用对应解压方法* @param filePath*/public static void uncompress(String filePath){String fileType = filePath.substring(filePath.length()-3, filePath.length());String dirPath = filePath.substring(0, filePath.length()-4);if("zip".equalsIgnoreCase(fileType)){try {unZipFiles(filePath, dirPath);} catch (Exception e) {e.printStackTrace();}}else if("rar".equalsIgnoreCase(fileType)){try {unRarFile(filePath, dirPath);} catch (Exception e) {e.printStackTrace();}}else {System.out.println("文件格式错误");}}/*** 解压zip格式的压缩文件到指定位置** @param zipFileName* 压缩文件* @param extPlace* 解压目录* @throws Exception*/@SuppressWarnings("unchecked")public static boolean unZipFiles(String zipFileName, String extPlace) throws Exception {System.setProperty("sun.zip.encoding",System.getProperty("sun.jnu.encoding"));try {(new File(extPlace)).mkdirs();File f = new File(zipFileName);ZipFile zipFile = new ZipFile(zipFileName, "GBK"); // 处理中文文件名乱码的问题if ((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");}String strPath, gbkPath, strtemp;File tempFile = new File(extPlace);strPath = tempFile.getAbsolutePath();Enumeration<?> e = zipFile.getEntries();while (e.hasMoreElements()) {ZipEntry zipEnt = (ZipEntry) e.nextElement();gbkPath = zipEnt.getName();if (zipEnt.isDirectory()) {strtemp = strPath + File.separator + gbkPath;File dir = new File(strtemp);dir.mkdirs();continue;} else { // 读写文件if(gbkPath.indexOf("fix") == -1){//fix文件不解压,解压出来的fix影响shape文件的正常解析InputStream is = zipFile.getInputStream(zipEnt);BufferedInputStream bis = new BufferedInputStream(is);gbkPath = zipEnt.getName();strtemp = strPath + File.separator + gbkPath;// 建目录String strsubdir = gbkPath;for (int i = 0; i < strsubdir.length(); i++) {if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {String temp = strPath + File.separator+ strsubdir.substring(0, i);File subdir = new File(temp);if (!subdir.exists())subdir.mkdir();}}FileOutputStream fos = new FileOutputStream(strtemp);BufferedOutputStream bos = new BufferedOutputStream(fos);int c;while ((c = bis.read()) != -1) {bos.write((byte) c);}bos.close();fos.close();}}}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据原始rar路径,解压到指定文件夹下.** @param srcRarPath* 原始rar路径* @param dstDirectoryPath* 解压到的文件夹*/public static void unRarFile(String srcRarPath, String dstDirectoryPath) throws Exception{if (!srcRarPath.toLowerCase().endsWith(".rar")) {System.out.println("非rar文件!");return;}File dstDiretory = new File(dstDirectoryPath);if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹dstDiretory.mkdirs();}File file = new File(srcRarPath);Archive a = null;try {a = new Archive(new FileInputStream(new File(srcRarPath)));if (a != null) {// a.getMainHeader().print(); // 打印文件信息.FileHeader fh = a.nextFileHeader();while (fh != null) {// 防止文件名中文乱码问题的处理String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW();if (fh.isDirectory()) { // 文件夹File fol = new File(dstDirectoryPath + File.separator+ fileName);fol.mkdirs();} else { // 文件if(fileName.indexOf("fix") == -1){//fix文件不解压,解压出来的fix影响shape文件的正常解析File out = new File(dstDirectoryPath + File.separator+ fileName.trim());try {if (!out.exists()) {if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.out.getParentFile().mkdirs();}out.createNewFile();}FileOutputStream os = new FileOutputStream(out);a.extractFile(fh, os);os.close();} catch (Exception ex) {ex.printStackTrace();}}}fh = a.nextFileHeader();}a.close();}} catch (Exception e) {e.printStackTrace();}}public static void unRar(String path, String outDir) throws Exception {File rarFile = new File(path);File outFileDir = new File(outDir);if (!outFileDir.exists()) {boolean isMakDir = outFileDir.mkdirs();if (isMakDir) {System.out.println("创建压缩目录成功");}}Archive archive = new Archive(new FileInputStream(rarFile));FileHeader fileHeader = archive.nextFileHeader();while (fileHeader != null) {String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();System.out.println(fileName);if (fileHeader.isDirectory()) {fileHeader = archive.nextFileHeader();continue;}fileHeader = archive.nextFileHeader();}archive.close();}/*** 获取文件路径* @param file* @param filePath* @return* @throws IOException*/public static String getPath(MultipartFile file, String filePath) throws IOException{String fileName = FileUploadUtils.upload(filePath, file);int index=fileName.indexOf("/");index=fileName.indexOf("/", index+1);index=fileName.indexOf("/", index+2);String fileNameSplit = fileName.substring(index);// String url = serverConfig.getUrl() + fileName;String allPath = filePath+fileNameSplit;return allPath;}public static String headName(MultipartFile file){System.out.println(file.getOriginalFilename().split("."));file.getOriginalFilename().split(".");return "";}public static void main(String[] args) {/*try {String path1 = "C:\\Users\\Administrator\\Desktop\\jzd.rar";String path2 = "C:\\Users\\Administrator\\Desktop\\rar";unRarFile(path1,path2);} catch (Exception e) {e.printStackTrace();}*/String path1 = "C:\\Users\\Administrator\\Desktop\\rar\\JZD.zip";String path2 = "C:\\Users\\Administrator\\Desktop\\rar\\JZD";try {unZipFiles(path1,path2);} catch (Exception e) {e.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。