Skip to content

Commit

Permalink
first import
Browse files Browse the repository at this point in the history
  • Loading branch information
markokr committed Dec 12, 2005
0 parents commit 975dbbc
Show file tree
Hide file tree
Showing 7 changed files with 789 additions and 0 deletions.
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

Copyright (c) 2005 Marko Kreen <[email protected]>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README Makefile MANIFEST.in LICENSE
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

prefix = /opt

all:
python setup.py build

install:
python setup.py install --prefix=$(prefix)

tgz:
python setup.py sdist

clean:
rm -rf *.pyc build dist MANIFEST *.orig *.rej *.html

docs:
asciidoc -b xhtml11 README

94 changes: 94 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

rarfile - Rar archive reader for Python
=======================================

1. Description
----------------

This is Python module for Rar archive reading. The interface
is made as `zipfile` like as possible.

Features:

- Supports RAR 3.x archives.
- Supports multi volume archives.
- Supports Unicode filenames.
- Handles non-compressed files without outside help.
- For compressed files runs `unrar` utility.

Missing features:

- Password-protected archives.
- Comment handling. (archive, file)
- Decompression through unrarlib and/or unrar.dll.
- extract_with_hack on multi-volume archives

Personally, I have no need for those, so ...


2. Module contents
--------------------

2.1. is_rarfile(file)
~~~~~~~~~~~~~~~~~~~~~~

Returns true is `file` is Rar archive.


2.2. class RarFile
~~~~~~~~~~~~~~~~~~~~

`RarFile(archive)`::
Creates new RarFile instance and reads Rar directory from archive.

namelist()::
Return list of filenames.

infolist()::
Return list of RarInfo items.

getinfo(filename)::
Returns a RarInfo instance for specific file.

read(filename)::
Read file contents.

close(filename)::
Free open resources. Currently it does nothing.


2.3. class RarInfo
~~~~~~~~~~~~~~~~~~~~

filename::
File name with relative path.
Note that Rar uses "\" as directory separator.

unicode_filename::
If Rar has s

host_os::
Host OS type.

file_size::
Uncompressed size.

date_time::
Tuple of year, month, day, hour, minute, second.

compress_size::
Compressed size.

compress_type::
Compression method: 0x30 - 0x35.

extract_version::
Minimal Rar version needed for decompressing.

mode::
File attributes. May be either dos-style or unix-style,
depending on host_os.

CRC::
CRC-32 of uncompressed file

120 changes: 120 additions & 0 deletions dumprar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#! /usr/bin/env python

import sys, os
from StringIO import StringIO
from array import array
from rarfile import *

os_list = ['DOS', 'OS2', 'WIN', 'UNIX']

block_strs = ['MARK', 'MAIN', 'FILE', 'OLD_COMMENT', 'OLD_EXTRA',
'OLD_SUB', 'OLD_RECOVERY', 'OLD_AUTH', 'SUB', 'ENDARC']

def rarType(type):
if type < RAR_BLOCK_MARK or type > RAR_BLOCK_ENDARC:
return "*UNKNOWN*"
return block_strs[type - RAR_BLOCK_MARK]

main_bits = (
(RAR_MAIN_VOLUME, "VOL"),
(RAR_MAIN_COMMENT, "COMMENT"),
(RAR_MAIN_LOCK, "LOCK"),
(RAR_MAIN_SOLID, "SOLID"),
(RAR_MAIN_NEWNUMBERING, "NEWNR"),
(RAR_MAIN_AUTH, "AUTH"),
(RAR_MAIN_RECOVERY, "RECOVERY"),
(RAR_MAIN_PASSWORD, "PASSWORD"),
(RAR_MAIN_FIRSTVOLUME, "FIRSTVOL"),
(RAR_SKIP_IF_UNKNOWN, "SKIP"),
(RAR_LONG_BLOCK, "LONG"),
)

endarc_bits = (
(RAR_ENDARC_NEXT_VOLUME, "NEXTVOL"),
(RAR_ENDARC_DATACRC, "DATACRC"),
(RAR_ENDARC_REVSPACE, "REVSPACE"),
(RAR_SKIP_IF_UNKNOWN, "SKIP"),
(RAR_LONG_BLOCK, "LONG"),
)

file_bits = (
(RAR_FILE_SPLIT_BEFORE, "SPLIT_BEFORE"),
(RAR_FILE_SPLIT_AFTER, "SPLIT_AFTER"),
(RAR_FILE_PASSWORD, "PASSWORD"),
(RAR_FILE_COMMENT, "COMMENT"),
(RAR_FILE_SOLID, "SOLID"),
(RAR_FILE_LARGE, "LARGE"),
(RAR_FILE_UNICODE, "UNICODE"),
(RAR_FILE_SALT, "SALT"),
(RAR_FILE_VERSION, "VERSION"),
(RAR_FILE_EXTTIME, "EXTTIME"),
(RAR_FILE_EXTFLAGS, "EXTFLAGS"),
(RAR_SKIP_IF_UNKNOWN, "SKIP"),
(RAR_LONG_BLOCK, "LONG"),
)

generic_bits = (
(RAR_SKIP_IF_UNKNOWN, "SKIP"),
(RAR_LONG_BLOCK, "LONG"),
)

file_parms = ("D64", "D128", "D256", "D512",
"D1024", "D2048", "D4096", "DIR")

def render_flags(flags, bit_list):
res = []
for bit in bit_list:
#val = 1 << bit[0]
if flags & bit[0]:
res.append(bit[1])
return ",".join(res)

def get_file_flags(flags):
res = render_flags(flags, file_bits)

xf = (flags & RAR_FILE_DICTMASK) >> 5
res += "," + file_parms[xf]
return res

def get_main_flags(flags):
return render_flags(flags, main_bits)

def get_endarc_flags(flags):
return render_flags(flags, endarc_bits)

def get_generic_flags(flags):
return render_flags(flags, generic_bits)

def fmt_time(t):
return "%04d-%02d-%02d %02d:%02d:%02d" % t

def show_item(h):
st = rarType(h.type)
print "%s: hdrlen=%d datlen=%d hdr_unknown=%d" % (st, h.header_size,
h.add_size, h.header_unknown)
if h.header_unknown > 0:
dat = h.data[-h.header_unknown:]
print " unknown:", repr(dat)
if h.type in (RAR_BLOCK_FILE, RAR_BLOCK_SUB):
if h.host_os == RAR_OS_UNIX:
s_mode = "0%o" % h.mode
else:
s_mode = "0x%x" % h.mode
print " flags=0x%04x:%s" % (h.flags, get_file_flags(h.flags))
print " os=%d:%s ver=%d mode=%s meth=%c cmp=%d dec=%d" % (
h.host_os, os_list[h.host_os],
h.extract_version, s_mode, h.compress_type,
h.compress_size, h.file_size)
print " crc=0x%08x time=%s" % (h.CRC, fmt_time(h.date_time))
print " name=%s" % h.unicode_filename
elif h.type == RAR_BLOCK_MAIN:
print " flags=0x%04x:%s" % (h.flags, get_main_flags(h.flags))
elif h.type == RAR_BLOCK_ENDARC:
print " flags=0x%04x:%s" % (h.flags, get_endarc_flags(h.flags))
else:
print " flags=0x%04x:%s" % (h.flags, get_generic_flags(h.flags))

for fn in sys.argv[1:]:
print "Rar:", fn
RarFile(fn, info_callback = show_item, charset="iso-8859-1")

Loading

0 comments on commit 975dbbc

Please sign in to comment.