最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓

 更新時(shí)間:2023年07月15日 11:24:57   作者:彭先生吖  
這篇文章主要介紹了java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓?jiǎn)栴},具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java zip rar 7z 壓縮包解壓

7z和rar需要引入maven依賴,zip使用java自帶的

<!-- 7z解壓依賴 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.tukaani</groupId>
            <artifactId>xz</artifactId>
            <version>1.5</version>
        </dependency>
      	<!-- rar解壓依賴 -- >
        <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>

編寫解壓util工具類

/**
 * 文件解壓縮工具類
 */	
public class UnFileUtil {
	/**
	 * rar解壓縮
	 * @param rarFile	rar文件的全路徑
	 * @return  壓縮包中所有的文件
	 */	
	@SuppressWarnings("resource")
	public static Map<String, String> unRar(String rarFile) {
		RandomAccessFile randomAccessFile = null;
		IInArchive archive = null;
		try {
			File f = new File(rarFile);
			randomAccessFile = new RandomAccessFile(f.getAbsolutePath(), "rw");
			archive = SevenZip.openInArchive(ArchiveFormat.RAR,
					new RandomAccessFileInStream(randomAccessFile));
			String outPath = f.getAbsolutePath().substring(0, f.getAbsolutePath().indexOf("."));
			File zdir = new File(outPath);
			if (zdir.isDirectory()) {
				zdir.delete();
			}
			zdir.mkdir();
			int[] in = new int[archive.getNumberOfItems()];
			for (int i = 0; i < in.length; i++) {
				in[i] = i;
			}
			archive.extract(in, false, new ExtractCallback(archive,zdir.getAbsolutePath() + "/"));
			//解壓后獲取壓縮包下全部文件列表
			Map<String, String> zipFileMap = getFileNameList(outPath,"");
			return zipFileMap;
		} catch (FileNotFoundException | SevenZipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(randomAccessFile != null) randomAccessFile.close();
				if(archive != null) archive.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 獲取壓縮包中的全部文件
	 * @param path	文件夾路徑
	 * @childName   每一個(gè)文件的每一層的路徑==D==區(qū)分層數(shù)
	 * @return  壓縮包中所有的文件
	 */	
	private static Map<String, String> getFileNameList(String path, String childName) {
		System.out.println("path:" + path + "---childName:"+childName);
		Map<String, String> files = new HashMap<>();
		File file = new File(path); // 需要獲取的文件的路徑
		String[] fileNameLists = file.list(); // 存儲(chǔ)文件名的String數(shù)組
		File[] filePathLists = file.listFiles(); // 存儲(chǔ)文件路徑的String數(shù)組
		for (int i = 0; i < filePathLists.length; i++) {
			if (filePathLists[i].isFile()) {
				files.put(fileNameLists[i] + "==D==" + childName, path + File.separator + filePathLists[i].getName());
			} else {
				files.putAll(getFileNameList(path + File.separator + filePathLists[i].getName(), childName + "&" + filePathLists[i].getName()));
			}
		}
		return files;
	}
	/**
	 * zip解壓縮
	 * @param zipFilePath			zip文件的全路徑
	 * @param unzipFilePath			解壓后文件保存路徑
	 * @param includeZipFileName	解壓后文件是否包含壓縮包文件名,true包含,false不包含
	 * @return 壓縮包中所有的文件
	 */
	@SuppressWarnings("resource")
	public static Map<String, String> unZip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception{
		File zipFile = new File(zipFilePath);
		//如果包含壓縮包文件名,則處理保存路徑
		if(includeZipFileName){
			String fileName = zipFile.getName();
			if(StringUtils.isNotEmpty(fileName)){
				fileName = fileName.substring(0,fileName.lastIndexOf("."));
			}
			unzipFilePath = unzipFilePath + File.separator + fileName;
		}
		//判斷保存路徑是否存在,不存在則創(chuàng)建
		File unzipFileDir = new File(unzipFilePath);
		if(!unzipFileDir.exists() || !unzipFileDir.isDirectory()){
			unzipFileDir.mkdirs();
		}
		//開(kāi)始解壓
		ZipEntry entry = null;
		String entryFilePath = null, entryDirPath = "";
		File entryFile = null, entryDir = null;
		int index = 0, count = 0, bufferSize = 1024;
		byte[] buffer = new byte[bufferSize];
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		Charset gbk = Charset.forName("GBK");
		ZipFile zip = new ZipFile(zipFile, gbk);
		try {
			Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
			while (entries.hasMoreElements()) {
				entry = entries.nextElement();
				entryFilePath = unzipFilePath + File.separator + entry.getName();
				entryFilePath = entryFilePath.replace("/", File.separator);
				index = entryFilePath.lastIndexOf(File.separator);
				if (index != -1) {
					entryDirPath = entryFilePath.substring(0, index);
				}
				entryDir = new File(entryDirPath);
				if (!entryDir.exists() || !entryDir.isDirectory()) {
					entryDir.mkdirs();
				}
				entryFile = new File(entryFilePath);
				//判斷當(dāng)前文件父類路徑是否存在,不存在則創(chuàng)建
				if(!entryFile.getParentFile().exists() || !entryFile.getParentFile().isDirectory()){
					entryFile.getParentFile().mkdirs();
				}
				//不是文件說(shuō)明是文件夾創(chuàng)建即可,無(wú)需寫入
				if(entryFile.isDirectory()){
					continue;
				}
				bos = new BufferedOutputStream(new FileOutputStream(entryFile));
				bis = new BufferedInputStream(zip.getInputStream(entry));
				while ((count = bis.read(buffer, 0, bufferSize)) != -1) {
					bos.write(buffer, 0, count);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			bos.flush();
			bos.close();
			bis.close();
			zip.close();
		}
		Map<String, String> resultMap = getFileNameList(unzipFilePath, "");
		return resultMap;
	}
	/**
	 * zip解壓縮
	 * @param zipFilePath			zip文件的全路徑
	 * @param includeZipFileName	解壓后文件是否包含壓縮包文件名,true包含,false不包含
	 * @return 壓縮包中所有的文件
	 */
	@SuppressWarnings("resource")
	public static Map<String, String> unZip(String zipFilePath, boolean includeZipFileName) throws Exception{
		File zipFile = new File(zipFilePath);
		String unzipFilePath = zipFilePath.substring(0, zipFilePath.lastIndexOf(File.separator));
		//如果包含壓縮包文件名,則處理保存路徑
		if(includeZipFileName){
			String fileName = zipFile.getName();
			if(StringUtils.isNotEmpty(fileName)){
				fileName = fileName.substring(0,fileName.lastIndexOf("."));
			}
			unzipFilePath = unzipFilePath + File.separator + fileName;
		}
		//判斷保存路徑是否存在,不存在則創(chuàng)建
		File unzipFileDir = new File(unzipFilePath);
		if(!unzipFileDir.exists() || !unzipFileDir.isDirectory()){
			unzipFileDir.mkdirs();
		}
		//開(kāi)始解壓
		ZipEntry entry = null;
		String entryFilePath = null, entryDirPath = "";
		File entryFile = null, entryDir = null;
		int index = 0, count = 0, bufferSize = 1024;
		byte[] buffer = new byte[bufferSize];
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		Charset gbk = Charset.forName("GBK");
		ZipFile zip = new ZipFile(zipFile, gbk);
		try {
			Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
			while (entries.hasMoreElements()) {
				entry = entries.nextElement();
				entryFilePath = unzipFilePath + File.separator + entry.getName();
				entryFilePath = entryFilePath.replace("/", File.separator);
				index = entryFilePath.lastIndexOf(File.separator);
				if (index != -1) {
					entryDirPath = entryFilePath.substring(0, index);
				}
				entryDir = new File(entryDirPath);
				if (!entryDir.exists() || !entryDir.isDirectory()) {
					entryDir.mkdirs();
				}
				entryFile = new File(entryFilePath);
				//判斷當(dāng)前文件父類路徑是否存在,不存在則創(chuàng)建
				if(!entryFile.getParentFile().exists() || !entryFile.getParentFile().isDirectory()){
					entryFile.getParentFile().mkdirs();
				}
				//不是文件說(shuō)明是文件夾創(chuàng)建即可,無(wú)需寫入
				if(entryFile.isDirectory()){
					continue;
				}
				bos = new BufferedOutputStream(new FileOutputStream(entryFile));
				bis = new BufferedInputStream(zip.getInputStream(entry));
				while ((count = bis.read(buffer, 0, bufferSize)) != -1) {
					bos.write(buffer, 0, count);
				}
				bos.flush();
				bos.close();
				bis.close();
			}
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			zip.close();
		}
		Map<String, String> resultMap = getFileNameList(unzipFilePath, "");
		return resultMap;
	}
	/**
	 * 7z解壓縮
	 * @param z7zFilePath	7z文件的全路徑
	 * @return  壓縮包中所有的文件
	 */
	public static Map<String, String> un7z(String z7zFilePath){
		String un7zFilePath = "";		//壓縮之后的絕對(duì)路徑
		SevenZFile zIn = null;
		try {
			File file = new File(z7zFilePath);
			un7zFilePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".7z"));
			zIn = new SevenZFile(file);
			SevenZArchiveEntry entry = null;
			File newFile = null;
			while ((entry = zIn.getNextEntry()) != null){
				//不是文件夾就進(jìn)行解壓
				if(!entry.isDirectory()){
					newFile = new File(un7zFilePath, entry.getName());
					if(!newFile.exists()){
						new File(newFile.getParent()).mkdirs();   //創(chuàng)建此文件的上層目錄
					}
					OutputStream out = new FileOutputStream(newFile);
					BufferedOutputStream bos = new BufferedOutputStream(out);
					int len = -1;
					byte[] buf = new byte[(int)entry.getSize()];
					while ((len = zIn.read(buf)) != -1){
						bos.write(buf, 0, len);
					}
					bos.flush();
					bos.close();
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (zIn != null)
					zIn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Map<String, String> resultMap = getFileNameList(un7zFilePath, "");
		return resultMap;
	}
	public static void main(String[] args) {
		try {
			un7z("C:\\Users\\Admin\\Desktop\\1716F0190017.7z");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

java常見(jiàn)壓縮包解壓工具類(支持:zip、7z和rar)

pom依賴

? ? ? ? <groupId>org.apache.commons</groupId>
? ? ? ? <artifactId>commons-compress</artifactId>
? ? ? ? <version>1.9</version>
? ? </dependency>
? ? <dependency>
? ? ? ? <groupId>org.tukaani</groupId>
? ? ? ? <artifactId>xz</artifactId>
? ? ? ? <version>1.5</version>
? ? </dependency>
? ? <!-- rar解壓依賴 -->
? <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>

工具類實(shí)現(xiàn)

     
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.github.yulichang.common.support.func.S;
import com.onemap.business.web.resultsCheck.service.impl.ResultPackageReportServiceImpl;
import com.onemap.common.utils.StringUtils;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.LongFunction;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
 * 文件解壓縮工具類
 */
public class UnPagFileUtil {
    private static final Logger log = LoggerFactory.getLogger(UnFileUtil.class);
    /**
     * rar解壓縮
     * @param rarFile    rar文件的全路徑
     * @param outPath  解壓路徑
     * @return  壓縮包中所有的文件
     */
    public static void unRarZip7Z(String rarFile, String outPath) throws Exception {
        if (rarFile.toLowerCase().endsWith(".zip")) {
            unZip(rarFile, outPath, false);
        } else if (rarFile.toLowerCase().endsWith(".rar")) {
            unRar(rarFile, outPath, "");
        } else if (rarFile.toLowerCase().endsWith(".7z")) {
            un7z(rarFile, outPath);
        }
    }
    /**
     * rar解壓縮
     * @param rarFile    rar文件的全路徑
     * @param outPath  解壓路徑
     * @return  壓縮包中所有的文件
     */
    private static String unRar(String rarFile, String outPath, String passWord) {
        RandomAccessFile randomAccessFile = null;
        IInArchive inArchive = null;
        try {
            // 第一個(gè)參數(shù)是需要解壓的壓縮包路徑,第二個(gè)參數(shù)參考JdkAPI文檔的RandomAccessFile
            randomAccessFile = new RandomAccessFile(rarFile, "r");
            if (StringUtils.isNotBlank(passWord)) {
                inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord);
            } else {
                inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
            }
            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
            for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                final int[] hash = new int[]{0};
                if (!item.isFolder()) {
                    ExtractOperationResult result;
                    final long[] sizeArray = new long[1];
                    File outFile = new File(outPath + File.separator+ item.getPath());
                    File parent = outFile.getParentFile();
                    if ((!parent.exists()) && (!parent.mkdirs())) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(passWord)) {
                        result = item.extractSlow(data -> {
                            try {
                                IOUtils.write(data, new FileOutputStream(outFile, true));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed
                        }, passWord);
                    } else {
                        result = item.extractSlow(data -> {
                            try {
                                IOUtils.write(data, new FileOutputStream(outFile, true));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed
                        });
                    }
                    if (result == ExtractOperationResult.OK) {
                        log.error("解壓rar成功...." + String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));
                    } else if (StringUtils.isNotBlank(passWord)) {
                        log.error("解壓rar成功:密碼錯(cuò)誤或者其他錯(cuò)誤...." + result);
                        return "password";
                    } else {
                        return "rar error";
                    }
                }
            }
        } catch (Exception e) {
            log.error("unRar error", e);
            return e.getMessage();
        } finally {
            try {
                inArchive.close();
                randomAccessFile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }
    /**
     * 獲取壓縮包中的全部文件
     * @param path    文件夾路徑
     * @childName   每一個(gè)文件的每一層的路徑==D==區(qū)分層數(shù)
     * @return  壓縮包中所有的文件
     */
    private static Map<String, String> getFileNameList(String path, String childName) {
        System.out.println("path:" + path + "---childName:"+childName);
        Map<String, String> files = new HashMap<>();
        File file = new File(path); // 需要獲取的文件的路徑
        String[] fileNameLists = file.list(); // 存儲(chǔ)文件名的String數(shù)組
        File[] filePathLists = file.listFiles(); // 存儲(chǔ)文件路徑的String數(shù)組
        for (int i = 0; i < filePathLists.length; i++) {
            if (filePathLists[i].isFile()) {
                files.put(fileNameLists[i] + "==D==" + childName, path + File.separator + filePathLists[i].getName());
            } else {
                files.putAll(getFileNameList(path + File.separator + filePathLists[i].getName(), childName + "&" + filePathLists[i].getName()));
            }
        }
        return files;
    }
    /**
     * zip解壓縮
     * @param zipFilePath            zip文件的全路徑
     * @param unzipFilePath            解壓后文件保存路徑
     * @param includeZipFileName    解壓后文件是否包含壓縮包文件名,true包含,false不包含
     * @return 壓縮包中所有的文件
     */
    public static Map<String, String> unZip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception{
        File zipFile = new File(zipFilePath);
        //如果包含壓縮包文件名,則處理保存路徑
        if(includeZipFileName){
            String fileName = zipFile.getName();
            if(StringUtils.isNotEmpty(fileName)){
                fileName = fileName.substring(0,fileName.lastIndexOf("."));
            }
            unzipFilePath = unzipFilePath + File.separator + fileName;
        }
        //判斷保存路徑是否存在,不存在則創(chuàng)建
        File unzipFileDir = new File(unzipFilePath);
        if(!unzipFileDir.exists() || !unzipFileDir.isDirectory()){
            unzipFileDir.mkdirs();
        }
        //開(kāi)始解壓
        ZipEntry entry = null;
        String entryFilePath = null, entryDirPath = "";
        File entryFile = null, entryDir = null;
        int index = 0, count = 0, bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        Charset gbk = Charset.forName("GBK");
        ZipFile zip = new ZipFile(zipFile, gbk);
        try {
            Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                entryFilePath = unzipFilePath + File.separator + entry.getName();
                entryFilePath = entryFilePath.replace("/", File.separator);
                index = entryFilePath.lastIndexOf(File.separator);
                if (index != -1) {
                    entryDirPath = entryFilePath.substring(0, index);
                }
                entryDir = new File(entryDirPath);
                if (!entryDir.exists() || !entryDir.isDirectory()) {
                    entryDir.mkdirs();
                }
                entryFile = new File(entryFilePath);
                //判斷當(dāng)前文件父類路徑是否存在,不存在則創(chuàng)建
                if(!entryFile.getParentFile().exists() || !entryFile.getParentFile().isDirectory()){
                    entryFile.getParentFile().mkdirs();
                }
                //不是文件說(shuō)明是文件夾創(chuàng)建即可,無(wú)需寫入
                if(entryFile.isDirectory()){
                    continue;
                }
                bos = new BufferedOutputStream(new FileOutputStream(entryFile));
                bis = new BufferedInputStream(zip.getInputStream(entry));
                while ((count = bis.read(buffer, 0, bufferSize)) != -1) {
                    bos.write(buffer, 0, count);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bos.flush();
            bos.close();
            bis.close();
            zip.close();
        }
        Map<String, String> resultMap = getFileNameList(unzipFilePath, "");
        return resultMap;
    }
    /**
     * zip解壓縮
     * @param zipFilePath            zip文件的全路徑
     * @param includeZipFileName    解壓后文件是否包含壓縮包文件名,true包含,false不包含
     * @return 壓縮包中所有的文件
     */
    public static Map<String, String> unZip(String zipFilePath, boolean includeZipFileName) throws Exception{
        File zipFile = new File(zipFilePath);
        String unzipFilePath = zipFilePath.substring(0, zipFilePath.lastIndexOf(File.separator));
        //如果包含壓縮包文件名,則處理保存路徑
        if(includeZipFileName){
            String fileName = zipFile.getName();
            if(StringUtils.isNotEmpty(fileName)){
                fileName = fileName.substring(0,fileName.lastIndexOf("."));
            }
            unzipFilePath = unzipFilePath + File.separator + fileName;
        }
        //判斷保存路徑是否存在,不存在則創(chuàng)建
        File unzipFileDir = new File(unzipFilePath);
        if(!unzipFileDir.exists() || !unzipFileDir.isDirectory()){
            unzipFileDir.mkdirs();
        }
        //開(kāi)始解壓
        ZipEntry entry = null;
        String entryFilePath = null, entryDirPath = "";
        File entryFile = null, entryDir = null;
        int index = 0, count = 0, bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        Charset gbk = Charset.forName("GBK");
        ZipFile zip = new ZipFile(zipFile, gbk);
        try {
            Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                entryFilePath = unzipFilePath + File.separator + entry.getName();
                entryFilePath = entryFilePath.replace("/", File.separator);
                index = entryFilePath.lastIndexOf(File.separator);
                if (index != -1) {
                    entryDirPath = entryFilePath.substring(0, index);
                }
                entryDir = new File(entryDirPath);
                if (!entryDir.exists() || !entryDir.isDirectory()) {
                    entryDir.mkdirs();
                }
                entryFile = new File(entryFilePath);
                //判斷當(dāng)前文件父類路徑是否存在,不存在則創(chuàng)建
                if(!entryFile.getParentFile().exists() || !entryFile.getParentFile().isDirectory()){
                    entryFile.getParentFile().mkdirs();
                }
                //不是文件說(shuō)明是文件夾創(chuàng)建即可,無(wú)需寫入
                if(entryFile.isDirectory()){
                    continue;
                }
                bos = new BufferedOutputStream(new FileOutputStream(entryFile));
                bis = new BufferedInputStream(zip.getInputStream(entry));
                while ((count = bis.read(buffer, 0, bufferSize)) != -1) {
                    bos.write(buffer, 0, count);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            zip.close();
        }
        Map<String, String> resultMap = getFileNameList(unzipFilePath, "");
        return resultMap;
    }
    /**
     * 7z解壓縮
     * @param z7zFilePath    7z文件的全路徑
     * @param outPath  解壓路徑
     * @return  壓縮包中所有的文件
     */
    public static Map<String, String> un7z(String z7zFilePath, String outPath){
        SevenZFile zIn = null;
        try {
            File file = new File(z7zFilePath);
            zIn = new SevenZFile(file);
            SevenZArchiveEntry entry = null;
            File newFile = null;
            while ((entry = zIn.getNextEntry()) != null){
                //不是文件夾就進(jìn)行解壓
                if(!entry.isDirectory()){
                    newFile = new File(outPath, entry.getName());
                    if(!newFile.exists()){
                        new File(newFile.getParent()).mkdirs();   //創(chuàng)建此文件的上層目錄
                    }
                    OutputStream out = new FileOutputStream(newFile);
                    BufferedOutputStream bos = new BufferedOutputStream(out);
                    int len = -1;
                    byte[] buf = new byte[(int)entry.getSize()];
                    while ((len = zIn.read(buf)) != -1){
                        bos.write(buf, 0, len);
                    }
                    bos.flush();
                    bos.close();
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (zIn != null) {
                    zIn.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String, String> resultMap = getFileNameList(outPath, "");
        return resultMap;
    }
    public static void main(String[] args) {
        try {
            String inputFile = "E:\\新建文件夾 (2)\\壓縮測(cè)試.zip";
            String inputFile2 = "E:\\新建文件夾 (2)\\紅光村批復(fù).rar";
            String inputFile3 = "E:\\新建文件夾 (2)\\壓縮測(cè)試.7z";
            String destDirPath = "E:\\新建文件夾\\zip";
            String destDirPath2 = "E:\\新建文件夾\\rar";
            String destDirPath3 = "E:\\新建文件夾\\7z";
            String suffix = inputFile.substring(inputFile.lastIndexOf("."));
            //File zipFile = new File("E:\\新建文件夾 (2)\\壓縮測(cè)試.7z");
            //un7z(inputFile3, destDirPath3);
            //unZip(inputFile, destDirPath, false);
            unRar(inputFile2, destDirPath2, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?六類運(yùn)算符詳解

    Java?六類運(yùn)算符詳解

    這篇文章主要介紹了Java?六類運(yùn)算符,在?Java?語(yǔ)言中,運(yùn)算符有算數(shù)運(yùn)算符、關(guān)系運(yùn)算符、邏輯運(yùn)算符、賦值運(yùn)算符、字符串連接運(yùn)算符、條件運(yùn)算符,感興趣的朋友可以閱讀一下
    2023-03-03
  • Java多線程窗口售票問(wèn)題實(shí)例

    Java多線程窗口售票問(wèn)題實(shí)例

    這篇文章主要介紹了Java多線程窗口售票問(wèn)題實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 使用java打印心型、圓形圖案的實(shí)現(xiàn)代碼

    使用java打印心型、圓形圖案的實(shí)現(xiàn)代碼

    這篇文章主要介紹了使用java打印心型、圓形圖案的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • 使用Spring實(shí)現(xiàn)@Value注入靜態(tài)字段

    使用Spring實(shí)現(xiàn)@Value注入靜態(tài)字段

    這篇文章主要介紹了使用Spring實(shí)現(xiàn)@Value注入靜態(tài)字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 詳解Javaweb狀態(tài)管理的Session和Cookie

    詳解Javaweb狀態(tài)管理的Session和Cookie

    這篇文章主要介紹了Javaweb狀態(tài)管理的Session和Cookie,將瀏覽器與web服務(wù)器之間多次交互當(dāng)做一個(gè)整體來(lái)處理,并且多次交互所涉及的數(shù)據(jù)(狀態(tài))保存下來(lái),需要的朋友可以參考下
    2023-05-05
  • 基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    這篇文章主要介紹了基于SpringBoot中activeMq的JmsTemplate的實(shí)例問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Spring中的@PathVariable注解詳細(xì)解析

    Spring中的@PathVariable注解詳細(xì)解析

    這篇文章主要介紹了Spring中的@PathVariable注解詳細(xì)解析,@PathVariable 是 Spring 框架中的一個(gè)注解,用于將 URL 中的變量綁定到方法的參數(shù)上,它通常用于處理 RESTful 風(fēng)格的請(qǐng)求,從 URL 中提取參數(shù)值,并將其傳遞給方法進(jìn)行處理,需要的朋友可以參考下
    2024-01-01
  • java字符串格式化(String類format方法)

    java字符串格式化(String類format方法)

    這篇文章主要介紹了java字符串格式化(String類format方法),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 解決maven中只有Lifecycle而Dependencies和Plugins消失的問(wèn)題

    解決maven中只有Lifecycle而Dependencies和Plugins消失的問(wèn)題

    這篇文章主要介紹了maven中只有Lifecycle而Dependencies和Plugins消失的問(wèn)題及解決方法,本文通過(guò)圖文的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-07-07
  • Java設(shè)置JSON字符串參數(shù)編碼的示例詳解

    Java設(shè)置JSON字符串參數(shù)編碼的示例詳解

    在Java中創(chuàng)建JSON字符串,我們可以使用多個(gè)庫(kù),其中最流行的是Jackson、Gson和org.json,,下面給大家分享Java設(shè)置JSON字符串參數(shù)編碼的示例,感興趣的朋友一起看看吧
    2024-06-06

最新評(píng)論

鄱阳县| 肇源县| 突泉县| 都安| 天峨县| 威信县| 五河县| 郴州市| 高州市| 长武县| 台南县| 博湖县| 廊坊市| 乐东| 邮箱| 克什克腾旗| 宽甸| 广平县| 宁强县| 扎兰屯市| 达州市| 新邵县| 玉林市| 腾冲县| 保康县| 建阳市| 牙克石市| 同江市| 哈巴河县| 温宿县| 天峨县| 开原市| 涞源县| 思南县| 随州市| 大同市| 桑植县| 阜城县| 罗平县| 漠河县| 叙永县|