java解压缩rar文件

说明:需要一个.dll动态链接库和一个jar包(见附件)。
附原文:
Unrar a RAR Archive
Open a RAR archive and unrar to a directory, recreating the directory tree as present in the RAR.

Note: The Chilkat RAR ActiveX objects are included in the "Chilkat Zip" ActiveX download. The RAR functionality contained within ChilkatZip2.dll is freeware, however, the Chilkat Zip, GZip, and .Z functionality is not freeware.

The Chilkat RAR for .NET and C++ are bundled in downloads that contain all Chilkat classes (both free and non-free). Make sure you select the download that matches your .NET Framework or VC++ version (6/7/8).

If running on x64, download the ActiveX, .NET, or C++ builds marked specifically for x64.

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
  //  此处很重要:不能出现.dll后缀名,需要将chilkat.dll存放
  //  在D:\Program  Files\Genuitec\Common\binary
  //  \com.sun.java.jdk.win32.x86_1.6.0.013\bin等目录下。
  //   不然报"no chilkat in java.library.path"错误
      System.loadLibrary("chilkat"); 

    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRar rar = new CkRar();

    //  Note: The Chilkat RAR functionality only provides the ability
    //  to open, list, and "unrar" (i.e. extract) RAR archives.  It does
    //  not provide the ability to create RAR archives.
    //  (只提供打开、浏览和解压rar文件,不能创建rar文件)。

    //  Also, the Chilkat RAR functionality is free.  It does not
    //  require a license to use indefinitely.(免费使用)

    boolean success;

    //rar.Open(filepath); filepath是需要解压的文件的完整路径和文件名。 
    success = rar.Open("abc123.rar");  
   
    if (success != true) {
        System.out.println(rar.lastErrorText());
        return;
    }

    //解压输出目录,此处为直接解压,类似于winrar的解压到当前文件夹。
     //如果目录文件夹不存在,不要事先创建。
    success = rar.Unrar("c:/temp/unrarDest/"); 
    if (success != true) {
        System.out.println(rar.lastErrorText());
    }
    else {
        System.out.println("Success.");
    }

  }
}


-------

Open RAR Archive and List Contents

Demonstrates how to open a RAR archive and list the contents.

Note: The Chilkat RAR ActiveX objects are included in the "Chilkat Zip" ActiveX download. The RAR functionality contained within ChilkatZip2.dll is freeware, however, the Chilkat Zip, GZip, and .Z functionality is not freeware.

The Chilkat RAR for .NET and C++ are bundled in downloads that contain all Chilkat classes (both free and non-free). Make sure you select the download that matches your .NET Framework or VC++ version (6/7/8).

If running on x64, download the ActiveX, .NET, or C++ builds marked specifically for x64.

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRar rar = new CkRar();

    //  Note: The Chilkat RAR functionality only provides the ability
    //  to open, list, and "unrar" (i.e. extract) RAR archives.  It does
    //  not provide the ability to create RAR archives.

    //  Also, the Chilkat RAR functionality is free.  It does not
    //  require a license to use indefinitely.

    boolean success;

    success = rar.Open("abc123.rar");
    if (success != true) {
        System.out.println(rar.lastErrorText());
        return;
    }

    int n;
    int i;

    n = (int) rar.get_NumEntries();
    for (i = 0; i <= n - 1; i++) {

        CkRarEntry entry;
        entry = rar.GetEntryByIndex(i);

        System.out.println(entry.filename());
        System.out.println(entry.get_UncompressedSize());
        System.out.println(entry.get_CompressedSize());

        if (entry.get_IsDirectory() == true) {
            System.out.println("(directory)");
        }

        if (entry.get_IsReadOnly() == true) {
            System.out.println("(read-only)");
        }

        System.out.println("----");

    }

  }
}


附:
System.load 和 System.loadLibrary详解
http://freesoftman.iteye.com/blog/425657

--------------

原文引自: http://www.example-code.com/java/rar_unrar.asp http://www.example-code.com/java/rar_list.asp

---------------

附件:Download Chilkat Java Library

你可能感兴趣的:(java,.net,asp.net,asp,vc++)