1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java实现解压(zip和rar文件)实例-解决中文乱码问题

java实现解压(zip和rar文件)实例-解决中文乱码问题

时间:2018-07-24 09:55:48

相关推荐

java实现解压(zip和rar文件)实例-解决中文乱码问题

1、实现说明:

在下面引入的包中可以看出:

1)压缩、解压zip是用到的apache 的zip包,需要在引入jar包(jar-ant.rar);

2)压缩、解压rar需要用到的是junrar,这里需要引入第三方jar包,我用到的是junrar-0.7.jar

下面的两个方法是实现解压文件的实例,压缩文件的实现其实也很简单,大家可以查查实现方式,在此我还是不关心压缩文件(相对来说用的少)

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;import com.github.junrar.Archive;import com.github.junrar.rarfile.FileHeader;/*** * @author liuBf * 类说明:解压文件公用类 **/public class UnZipOrRarUtils {/*** 这里用到了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);}/*** 解压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 { // 读写文件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) {if (!srcRarPath.toLowerCase().endsWith(".rar")) {System.out.println("非rar文件!");return;}File dstDiretory = new File(dstDirectoryPath);if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹dstDiretory.mkdirs();}Archive a = null;try {a = new Archive(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 { // 文件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();}}}

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