java关于压缩包的处理,我这里贴出三中格式的(zip,rar,tar)解压方法(目前只用到解压,所以压缩过多研究)。

1、首先我们先来说下为什么会有这三中格式:

(1)rar格式:rar格式是最先出现的压缩方式,它主要是用于商业机构一些文件的压缩,它可以根据不同公司的要求,去设定制定不同的压缩算法,这种算法是不对外公开的,安全性比较高,但他是收费的。

(2)zip格式:因为rar格式收费,必然会诞生一些免费的压缩格式,那么zip就是在这样的情况下诞生的。同样压缩算法也是公开的,不收取任何费用,效果当然也比rar格式稍差。

(3)tar格式:tar是国产推出的一种免费的压缩格式。

2、java编程实现rar,zip,tar格式的解压缩。

(1)rar解压缩:这里需要用到junrar-0.7.jar包,这里是下载地址:http://pan.baidu.com/s/1mg80F1I

/**
     * 
     * 对rar文件进行解压
     * <p>描述</p>
     * @date 2014-7-16 下午1:59:28
     * @version 
     * @param sourceRar
     * @param destDir
     * @throws Exception
     */
    public static void unRar(String sourceRar, String destDir) 
               throws Exception {
        Archive a = null;
        FileOutputStream fos = null;
        try {
            a = new Archive(new File(sourceRar));
            FileHeader fh = a.nextFileHeader();
            
            while (fh != null) {
                if (!fh.isDirectory()) {
                     // 1 根据不同的操作系统拿到相应的 destDirName 和 destFileName
                    String compressFileName = fh.getFileNameW().trim();
                    String destFileName = "";
                    String destDirName = "";
                     // 非windows系统
                    if (File.separator.equals("/")) {
                      destFileName = destDir +"//"+ compressFileName.replaceAll("\\", "/");
                      destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
                      // windows系统
                     } else {
                      destFileName = destDir+"//"+ compressFileName.replaceAll("/", "\\");
                      destDirName = destFileName.substring(0,destFileName.lastIndexOf("\"));
                     }
                     // 2创建文件夹
                     File dir = new File(destDirName);
                     if (!dir.exists() || !dir.isDirectory()) {
                      dir.mkdirs();
                     }
                     // 3解压缩文件
                     fos = new FileOutputStream(new File(destFileName));
                     a.extractFile(fh, fos);
                     fos.close();
                     fos = null;
                }
                fh = a.nextFileHeader();
            }
            a.close();
            a = null;
        } catch (Exception e) {
            log.error("the methord unRar is fiail ", e);
            throw new Exception("Rar格式解压失败!");
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                    fos = null;
                }
                if (a != null) {
                    a.close();
                    a = null;
                }
            } catch (Exception e) {
                log.error("the methord unRar close is fiail ", e);
            }
            
            
        }
    }

(2)zip解压缩:这个直接用apache自带的。

/**
     * 解压缩ZIP文件,将ZIP文件里的内容解压到descFileName目录下
     * @param zipFileName 需要解压的ZIP文件
     * @param descFileName 目标文件
     */
    public static boolean unZipFiles(String zipFileName, String descFileName) {
        String descFileNames = descFileName;
        if (!descFileNames.endsWith(File.separator)) {
            descFileNames = descFileNames + File.separator;
        }        
        try {
            
            // 根据ZIP文件创建ZipFile对象
            ZipFile zipFile = new ZipFile(new File(zipFileName),"GB2312"); // 2014/6/27  解压乱码   杨云霞修改
            ZipEntry entry = null;
            String entryName = null;
            String descFileDir = null;
            byte[] buf = new byte[4096];
            int readByte = 0;
            // 获取ZIP文件里所有的entry
            @SuppressWarnings("rawtypes")
            Enumeration enums = zipFile.getEntries();
            // 遍历所有entry
            while (enums.hasMoreElements()) {
                entry = (ZipEntry) enums.nextElement();
                // 获得entry的名字
                entryName = entry.getName();
                descFileDir = descFileNames + entryName;
                if (entry.isDirectory()) {
                    // 如果entry是一个目录,则创建目录
                    new File(descFileDir).mkdirs();
                    continue;
                } else {
                    // 如果entry是一个文件,则创建父目录
                    new File(descFileDir).getParentFile().mkdirs();
                }
                File file = new File(descFileDir);
                // 打开文件输出流
                OutputStream os = new FileOutputStream(file);
                // 从ZipFile对象中打开entry的输入流
                InputStream is = zipFile.getInputStream(entry);
                while ((readByte = is.read(buf)) != -1) {
                    os.write(buf, 0, readByte);
                }
                os.close();
                is.close();
            }
            zipFile.close();
        //    log.debug("文件解压成功!");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

(2)tar解压缩:这里需要用到java-gnu-tar-0.0.1.jar包,这里是下载地址:http://pan.baidu.com/s/1sjCuquT

/**
     * 对tar文件进行解压缩
     * 描述内容
     * <p>描述</p>
     * @date 2014-7-16 下午2:00:30
     * @version 
     * @param path
     */
    public static void unTar(String path,String destDir) throws Exception { 
        InputStream inputstream = null; 
        OutputStream outputstream = null; 
        TarInputStream zis = null; 
        try {
            inputstream = new FileInputStream(new File(path)); 
            zis = new TarInputStream(inputstream); 
            
            //压缩包TarEntry,有多个文件(包含文件夹)就有多少个tarEntry
            TarEntry tarEntry = null; 
            String destFileName = "";
            String destDirName =""; 
            while ((tarEntry = zis.getNextEntry()) != null) {
                destFileName = destDir+"\" + tarEntry.getName().replaceAll("/", "\\");//获取文件地址
                destDirName = destFileName.substring(0,destFileName.lastIndexOf("\"));//获取文件目录
                File dir = new File(destDirName);
                if (!dir.exists() || !dir.isDirectory()) {
                  dir.mkdirs();
                }
                //判断是否为目录
                if(!destFileName.substring(0, destFileName.length()-1).equals(destDirName)){
                    outputstream = new FileOutputStream(new File(destFileName)); 
                    //定一个缓存池 可以根据实际情况调整大小(事实证明很有用) 
                     byte[] buffer = new byte[1024 * 100];
                    while (true) { 
                    int readsize = zis.read(buffer); 
                    outputstream.write(buffer); 
                        if (readsize < 1024 * 50) { 
                            break; 
                        } 
                    } 
                }
            } 
        }catch (IOException e) { 
            log.error("the methord unTar is fiail ", e);
            throw new Exception("Tar格式解压失败!");
        } finally { 
            try {
                if(outputstream!=null){
                    outputstream.flush(); 
                    outputstream.close(); 
                }
                if(inputstream!=null)
                    inputstream.close(); 
                if(zis!=null)
                    zis.close(); 
            } catch (Exception e) { 
                log.error("the methord unTar close is fiail ", e);
            } 

        } 


    } 

tar解压缩有中文乱码,一直没找到合适的处理办法,非常抱歉若有好办请提醒我。

仅铭记,本人好忘

  

原文地址:https://www.cnblogs.com/puqiuxiaomao/p/3853929.html