Like Tree3Likes
  • 2 Post By dwks
  • 1 Post By c99tutorial

Decompressing archives (7z, zip, rar) in C

This is a discussion on Decompressing archives (7z, zip, rar) in C within the C Programming forums, part of the General Programming Boards category; What is the simplest way to extract from archives, and create archives, I need to be able to support 7z, ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    28

    Decompressing archives (7z, zip, rar) in C

    What is the simplest way to extract from archives, and create archives, I need to be able to support 7z, zip, and rar files, and I really don't need to do anything complicated, just list the files that are in the archive, and extract a specific file. I was thinking of just using fork() and having the child process execute a 7z command, however that requires a lot of work with piping the output to some kind of string then parsing the string to figure out what's happened, and it seems to be a real dirty way of doing it, I'd like some kind of library that can do it.

    I've looked at the 7zip SDK, but I can't find hardly any documentation on it, and I don't even know what files it supports, if it just supports 7z out of the box, or all of the archives, and I don't want to spend too much time figuring out a library if I don't know it will do what I need.

    And like I mentioned, I don't need a lot of complex features, I just need to be able to list archive contents, extract all or specific items, and create an archive, and I need to support at least zip, 7z, and rar.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    I don't think you'll be able to support creating RAR archives. It's not an open format. There are plenty of libraries around for unrariing files though.

    There are plenty of libraries that support one or the other or all (except RAR creation). The 7zip SDK supports all that, but would probably take a while to figure out. I think it's probably your best bet - there don't seem to be many 7z libraries out there. Probably better to master one library than to try to munge several together.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    libarchive can extract a ton of different formats (not 7z though), and can make archives in some formats too. libarchive - C library and command-line tools for reading and writing tar, cpio, zip, ISO, and other archive formats @ GitHub

    Here's a list of the formats (from here):
    * tar (read and write, including GNU extensions)
    * pax (read and write, including GNU and star extensions)
    * cpio (read and write, including odc and newc variants)
    * iso9660 (read and write, including Joliet and Rockridge extensions, with
    some limitations)
    * zip (read only, with some limitations, uses zlib)
    * mtree (read and write)
    * shar (write only)
    * ar (read and write, including BSD and GNU/SysV variants)
    * empty (read only; in particular, note that no other format will accept an
    empty file)
    * raw (read only)
    * xar (read only)
    * rar (read only, with some limitations)
    * 7zip (read and write, with some limitations)
    Salem and KoRn KloWn like this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    333
    There's no easy answer. You can spawn a third party decompressor, but that's delicate, it will break if the user sets up his machine differently. Or you can write decompressor functions in C. However that's a lot of code. You can almost certainly find libraries on the net that will handle the formats you want, but it's non-trivial actually integrating them into a program of your own.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    28
    Quote Originally Posted by dwks View Post
    libarchive can extract a ton of different formats (not 7z though), and can make archives in some formats too. libarchive - C library and command-line tools for reading and writing tar, cpio, zip, ISO, and other archive formats @ GitHub

    Here's a list of the formats (from here):
    libarchive seems like a pretty good choice, especially because it's redily available on Linux, and right in the Ubuntu repositories, that works for me because I'm writing an app for Linux. The only issue is that it can't write zip files. I'm working on a comic library program for Linux, and it's fine to have read-only support for cbr files, however I need to have read/write support for cbz files, I can't just support creating cb7.

    I'm gonna keep looking around.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    28
    You know, now that I've been looking more at libarchive, it actually looks like it does support read and write, it someone just needs to update the description of it, and it looks pretty easy to use also, thanks for pointing out that library!

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    28
    Libarchive is definately going to be a winner, and the github page has decent documentation and examples so I know I can learn it, however, I can't find anywhere what header files to include to use the different functions? The only one I could find was archive.h, libarchives manpages don't seem to tell you what header files you need to use them. Any idea where I might find documentation that tells you more about the header files? I just need to know what to include, and if there are any flags I need to compile my program?

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,209
    In the manpage for archive_entry, for example, it says which header files as well as the linker flag (-larchive). Maybe the packages you got for Ubuntu didn't include all the files. The file I tried was libarchive-3.0.4.tar.gz from the homepage - libarchive - C library and command-line tools for reading and writing tar, cpio, zip, ISO, and other archive formats @ GitHub

    In there is also a complete example program, untar.c - which uses the library to decompress the tar archive given to it in stdin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Security type in zip archives ?
    By khpuce in forum Tech Board
    Replies: 1
    Last Post: 01-15-2008, 10:00 AM
  2. Post a thread from the archives
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-21-2004, 12:50 PM
  3. archives
    By smlbstcbr in forum C++ Programming
    Replies: 7
    Last Post: 11-02-2003, 12:16 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21