AD

Extract the zip and rar files Java

This article talk about how to implement in java on the zip and unzip rar files.
This article related to the source: source download 1, extract the rar file.
As WinRAR is shareware, not open source, so extracting rar file on the premise that the system is already installed winrar, such as my installation path is:
C: \ \ Program Files \ \ WinRAR \ \ winrar.exe
Then use the relevant knowledge java.lang.Process operating system command line to achieve decompression.
winrar command line parameters that they can search online information under many specific code:
**
 *  Extract rar files  ( You need Winrar software system installed  )
 * @author Michael sun
 */
public class UnRarFile {
    /**
     *  Extract rar files  
     * 
     * @param targetPath
     * @param absolutePath
     */
    public void unRarFile(String targetPath, String absolutePath) {

        try {

            //  The system path of the installed winrar  
            String cmd = "C:\\Program Files\\WinRAR\\winrar.exe";
            String unrarCmd = cmd + " x -r -p- -o+ " + absolutePath + " "
                    + targetPath;

            Runtime rt = Runtime.getRuntime();
            Process pre = rt.exec(unrarCmd);
            InputStreamReader isr = new InputStreamReader(pre.getInputStream());
            BufferedReader bf = new BufferedReader(isr);
            String line = null;
            while ((line = bf.readLine()) != null) {
                line = line.trim();
                if ("".equals(line)) {
                    continue;
                }
                System.out.println(line);
            }

            bf.close();
        } catch (Exception e) {
            System.out.println(" Decompression exception  ");
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String targetPath = "D:\\test\\unrar\\";
        String rarFilePath = "D:\\test\\test.rar";
        UnRarFile unrar = new UnRarFile();
        unrar.unRarFile(targetPath, rarFilePath);
    }

}

Second, extract the zip file because zip is free of charge, so provided in the jdk in the corresponding class of the zip files to achieve:
java.util.zip .*, details can refer to the java API
/**
 *  Extract the zip file  
 * @author Michael sun
 */
public class UnzipFile {

    /**
     *  Extract the zip file  
     * 
     * @param targetPath
     * @param zipFilePath
     */
    public void unzipFile(String targetPath, String zipFilePath) {

        try {
            File zipFile = new File(zipFilePath);
            InputStream is = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry = null;
            System.out.println(" Start decompression  :" + zipFile.getName() + "...");
            while ((entry = zis.getNextEntry()) != null) {
                String zipPath = entry.getName();
                try {

                    if (entry.isDirectory()) {
                        File zipFolder = new File(targetPath + File.separator
                                + zipPath);
                        if (!zipFolder.exists()) {
                            zipFolder.mkdirs();
                        }
                    } else {
                        File file = new File(targetPath + File.separator
                                + zipPath);
                        if (!file.exists()) {
                            File pathDir = file.getParentFile();
                            pathDir.mkdirs();
                            file.createNewFile();
                        }

                        FileOutputStream fos = new FileOutputStream(file);
                        int bread;
                        while ((bread = zis.read()) != -1) {
                            fos.write(bread);
                        }
                        fos.close();

                    }
                    System.out.println(" Successfully extract  :" + zipPath);

                } catch (Exception e) {
                    System.out.println(" Extract "+ zipPath +" failed  ");
                    continue;
                }
            }
            zis.close();
            is.close();
            System.out.println(" Extract the end  ");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String targetPath = "D:\\test\\unzip";
        String zipFile = "D:\\test\\test.zip";
        UnzipFile unzip = new UnzipFile();
        unzip.unzipFile(targetPath, zipFile);
    }

}
标签: java util, string args, public void, jdk, main string, installation path, d test, c program, zip file, relevant knowledge, java api, string line, getinputstream, command line parameters, string cmd, michael sun, author michael, decompression winrar, rar file, rar files
分类: Java
时间: 2010-03-31

相关文章

  1. java extract the zip and rar

    Word: zip free, rar algorithm fees. Here is one pair of zip, rar to extract the program (from http://topic.csd ...
  2. Extract the zip rat files under Linux

    linux unzip zip file with the unzip command, its format is as follows: unzip command Syntax: unzip [options] a ...
  3. Ubuntu Extract rar files under way

    Usually installed by default ubuntu can not extract the rar files, only installed the rar extracting tools, we ...
  4. ant to extract the zip using java

    Ant to extract the zip using very simple import org.apache.tools.ant.Project; import org.apache.tools.ant.task ...
  5. Compression and decompression under Linux. Zip and. Rar and .7 z file

    For Window under the common compressed file. Zip and. Rar, Linux also has a corresponding way to extract them: ...
  6. PHP执行zip与rar解压缩方法实现代码

    PHP执行zip与rar解压缩方法 先放着,以后研究,有地方要用到. Zip:PclZip http://www.phpconcept.net/pclzip/index.en.php Rar:PECL rar http: ...
  7. Read attributes of several files JAVA implementations

    Reprinted: http://darkranger.javaeye.com/blog/352857 Read attributes of several files JAVA implementations (20 ...
  8. Access restrestriction on required library C: \ Program Files \ Java \ jre6 \ lib \ rt.jar

    Access restriction: The type JPEGCodec is not accessible due to restriction on required library C: \ Program F ...
  9. JAVA extract rar files

    import java.io.BufferedReader; import java.io.InputStreamReader; /** * @author zhaoliangyuan * @E-mail zhao198 ...
  10. Ubuntu archive manager to extract the rar files

    1. To the official website RAR download rar for linux http://www.rarlab.com/download.htm 2. Unzip, modify make ...
  11. java extract the zip file compression

    Transfer from: http://wlh.javaeye.com/ import java.io.File; import java.io.FileInputStream; import java.io.Fil ...
  12. Ubuntu10.4 Chinese garbled extract rar files

    sudo apt-get remove rar sudo apt-get install unrar Those who previously installed the rar and unrar friends, e ...
  13. Extract rar files under ubuntu file name garbled, after the solution

    1 Solution: Install convmv the windows encoding format gbk, while ubuntu uses utf8, use this command to solve ...
  14. Chinese extract the zip file android

    Android platform is still a lot of the underlying libraries such as zlib library imperfections Zip files can n ...
  15. Use 7zip installed on Linux VPS Tutorials and extract the zip file name garbled Chinese solution

    7zip is an open source compression software solutions, not only their own unique 7z format, and supports zip, ...
  16. Extract the zip file under Linux

    unzip-t file.zip, used to check the zip file, which also lists the file. unzip file.zip, extract the files.
  17. method of reading and writing xml files java

    In java read xml files under the main methods for 4: DOM, SAX, JDOM, JAXB 1. DOM (Document Object Model) This ...
  18. Zip and rar compressed format the difference between

    One difference, zip large installation, and only available in English + finished package rar there is an offic ...
  19. VB Shell to achieve compression and decompression (Zip or Rar)

    Private Sub ZipOrRar () 'The C drive of the test folder compressed files test.rar Shell App.Path + "\ Win ...