extracting rar files in cygwin


I am new to cygwin, less than a week and I must say it is great.  I’m not too familiar with linux either other than change dir, remove dir or file commands.   But being able to run a Unix emulator such as cygwin is a great way to get familiarized with the unix environment.

That said, you are probably in the same shoes I found myself in a few minutes ago.   During my install of cygwin, I installed 7zip.  this is an archiving and compressed file extracting utility.   A recent task of mine was to extract text files from compressed formats and perform a line count.   Most of the files were .zip files until I came across some .rar files.  I thought 7zip would handle them since it handles .rar files via the windows interface, but instead I received an error.

$ 7z e test.rar

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=C,Utf16=off,HugeFiles=on,2 CPUs)
Can't load '/usr/lib/p7zip/Codecs/.keep-p7zip' (Permission denied)

Processing archive: test.rar

Extracting  test_out.txt     Unsupported Method

Sub items Errors: 1

The correct command to issue was unrar e test.rar.  But without having that RAR utility for cygwin and cygwin not not offering it through their regular setup or additional installs you need to take the following steps to install RAR on cygwin.

  1. Click on setup.exe in your original cygwin folder.  Usually it is c:\cygwin.
  2. Run through the setup as you did the first time until you come to the “Select Packages” screen.
  3. From the packages listed, select make and makedepend.  These packages are listed under Devel (for development).  Finish out the wizard (note – you will not lose anything you had previously installed).
  4. Go to http://www.rarlab.com/rar_add.htm and download the UNRar source files
  5. Save and extract the contents of the package under your home directory of your cygwin environment. (Once extracted, a folder named unrar will hold all the files)
  6. Start up your cygwin environment
  7. navigate to the unrar folder that contains all the source files for unrar.  Then type make -f makefile.cygmin.  This is the name of the file that will create an unrar.exe file in that same directory.  Your cygwin console will inform you when it is complete.  it usually takes a minute or two.
  8. Copy the resulting unrar.exe file to your c:\cygwin\bin\ directory (or wherever your root cygwin directory is).
  9. Now you are ready to uncompress your .rar file.
  10. To do this, navigate to the directory holding your .rar file.
  11. Type:  unrar e name_of_rar_file.rar

That’s it.  Once you use this, you will love it.  The performance is great.

In response to the comments made below, I’ve attached a screenshot of the gcc components that I have installed that allow the users to create the unrar.exe file to be placed in your bin.   Also here is a link to download my unrar.exe (compiled on my Windows 7 machine) file in case anyone wants to bypass the compiling step.  I know I am trustworthy, but always virus scan anything you download from the web.

gcc components installed in cygwin to get rar to work

gcc components installed in cygwin to get rar to work

About these ads

24 Comments

Filed under Sharing Stuff

24 responses to “extracting rar files in cygwin

  1. frisbeebk

    Thanks for the information. Just a note to help some, the source code is compiled using g++ therefor the gcc compiler must also be installed for these instructions to be successful.

  2. Clayton

    This doesn’t cut it for me: am I doing something wrong?

    $ make -f makefile.cygmin
    g++ -mno-cygwin -O2 -Wno-deprecated -D_MSC_VER -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DUNRAR -c rar.cpp
    g++: The -mno-cygwin flag has been removed; use a mingw-targeted cross-compiler.

    • Sorry I could not replicate your error, however as one person commented, you need to have the gcc compiler installed. I noticed that on my installation of cygwin, I have the following gcc components installed (see screenshot at bottom of post). The example was based off of an XP machine that I run at work, however at home I have a Windows 7 machine that I recently installed cygwin on and tried out.

      At the bottom of the post I’ve placed a screenshot of the gcc packages I have installed.

    • David Billinghurst

      I had success using the unix makefile on cygwin with the current cygwin g++ 4.3.4.

      make -f makefile.unix

      • Yep that will work. The exe will have a depenancy on cygwin.dll (so you can’t use it on a machine without copying that as well). Or if you install MinGW you can change the g++ in the makefile.cygwin to mingw32-g++.

        Thanks for the post BTW. I had a 50 rar files to extract and was surprized cygwin didn’t have unrar.

  3. ilias

    Compiled unrarsrc-4.0.3.tar.gz with gcc version 3.4.4 (make
    -f makefile.unix) and it worked. Thanks!

  4. Bryan

    Thanks… I’m trying to update my haipad tablet to android 2.2. the utility to update the firmware was rar’d. Your blog just made my life easier

  5. Mr. Krolik

    The task can be solved more easily due to the following symbolic link

    ln -s “/cygdrive/c/Program Files (x86)/WinRAR/Rar.exe” /usr/bin/rar

    if WinRAR is already installed in your host-system (i.e. Windows 7). After creating of symbolic link like this the rar-command will be available in xterm under sygwin, and you can work with the RAR-archives directly from bash-scripts as well as under Linux Operating System itself.

  6. sam

    ln -s to a native win32 binary will work but create problem when doing something like this

    $ ./unrar.exe /cygdrive/j/cc/rarfile2extract.rar

    or

    $ find . -type f -name “*.rar” | xargs ….

    you get the point.

  7. You don’t need to compile unrar from source.

    http://gnuwin32.sourceforge.net/packages/unrar.htm

    On my Win7 x64 box, unrar.exe was installed in “C:\Program Files (x86)\GnuWin32\bin”. You can then run the executable from the cygwin bash CLI, by either (1) prepending the path, or (2) adding the path to the Windows “Path” environment variable. You will have to relaunch cygwin after the change for (2) to work.

  8. Kryztoval

    I seem to have read this wrong… I installed p7zip from cygwin, and used 7z e as you said you did… it worked perfectly, but I wanted x instead of e. to preserve fodler structure.

    Amazing to me is the fact that your post acutally said that 7z should not work and I understood it the otherway around and it worked.

    thanks for the post :D

  9. Max

    I had to extract a 4.4 GB backup we were sent in a .rar file… it worked like a charm. :)
    I’d just add the source of unrar I got didn’t contain a “makefile.cygmin” file, but in my case the right one was “makefile.unix”
    thanks again!

  10. Hello! First of all, amazing post, thanks for sharing this info!
    I would like to mention that my cygwin folder did not have the setup file, but I did copy it myself there after seeing from your post just how useful that is!

    That being said,I followed the steps in your post, as well as the tips in the comments, but I still ran into the this problem:

    $ make -f makefile.unix
    g++ -O2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DUNRAR -c rar.cpp
    make: g++: Command not found
    makefile.unix:108: recipe for target `rar.o’ failed
    make: *** [rar.o] Error 127

    After some googling, I managed to fix it by installing g++, from the same Cygwin setup!

    Hopefully my comment will be helpful to others facing the same problem!

  11. ezak

    is there anyway to make rar command working in cygwin to add archive ?

    • Not as of now, but if I have some time I’ll try to come up with a work around and update you.

      Also, consider using gzip. That, in my opinion is the overall best compression utility in cygwin.

  12. It worked for me with cygwin using the default gcc.
    Thanks and great instructions

  13. I downloaded the latest unrar source from http://www.rarlab.com/rar/unrarsrc-5.0.14.tar.gz however there do not seem to be any makefiles!

  14. Dan

    Yes there’s only one makefile. And I had to modify it slightly to get it to compile (I’m using g++ 4.5.3):

    1. Open makefile and search for “-pthread” (should only appear once). Change this to “-lpthread”.
    2. Type make

Comment on this

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s