Extracting RAR files from Java code

Source: Internet
Author: User
Tags rar

  1. [Java]View Plaincopy
    1. Import Java.io.File;
    2. Import Java.io.FileOutputStream;
    3. Import de.innosystec.unrar.Archive;
    4. Import De.innosystec.unrar.rarfile.FileHeader;
    5. Public class Unrartools {
    6. public void Unrar (file sourcerar, file DestDir) throws Exception {
    7. Archive Archive = null;
    8. FileOutputStream FOS = null;
    9. System.out.println ("Starting ...");
    10. try {
    11. Archive = New archive (Sourcerar);
    12. Fileheader fh = Archive.nextfileheader ();
    13. int count = 0;
    14. File destfilename = null;
    15. While (fh! = null) {
    16. System.out.println ((++count) + ")" + fh.getfilenamestring ());
    17. String compressfilename = fh.getfilenamestring (). Trim ();
    18. destFileName = New File (Destdir.getabsolutepath () + "/" + compressfilename);
    19. if (fh.isdirectory ()) {
    20. if (!destfilename.exists ()) {
    21. Destfilename.mkdirs ();
    22. }
    23. FH = Archive.nextfileheader ();
    24. continue;
    25. }
    26. if (!destfilename.getparentfile (). exists ()) {
    27. Destfilename.getparentfile (). Mkdirs ();
    28. }
    29. FOS = new FileOutputStream (destFileName);
    30. Archive.extractfile (FH, FOS);
    31. Fos.close ();
    32. FOS = null;
    33. FH = Archive.nextfileheader ();
    34. }
    35. Archive.close ();
    36. archive = null;
    37. System.out.println ("finished!");
    38. } catch (Exception e) {
    39. throw E;
    40. } finally {
    41. if (fos! = null) {
    42. try {
    43. Fos.close ();
    44. FOS = null;
    45. } catch (Exception e) {
    46. //ignore
    47. }
    48. }
    49. if (archive! = null) {
    50. try {
    51. Archive.close ();
    52. archive = null;
    53. } catch (Exception e) {
    54. //ignore
    55. }
    56. }
    57. }
    58. }
    59. }

    A reference to the following two Lib is required.
    Java-unrar-0.5.jar
    Http://www.java2s.com/Code/JarDownload/java/java-unrar-0.5.jar.zip
    Apache-commons-logging.jar
    Http://www.java2s.com/Code/JarDownload/apache-commons/apache-commons-logging.jar.zip

Extracting RAR files from Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.