pax_global_header00006660000000000000000000000064124343110520014505gustar00rootroot0000000000000052 comment=3fa9e47fefc5b33888decf57cf13ff488b55261f rarfile-rarfile_2_7/000077500000000000000000000000001243431105200145665ustar00rootroot00000000000000rarfile-rarfile_2_7/LICENSE000066400000000000000000000013641243431105200155770ustar00rootroot00000000000000 Copyright (c) 2005-2013 Marko Kreen Permission to use, copy, modify, and/or 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. rarfile-rarfile_2_7/MANIFEST.in000066400000000000000000000002651243431105200163270ustar00rootroot00000000000000include README.rst Makefile MANIFEST.in LICENSE dumprar.py include doc/*.rst doc/Makefile doc/conf.py doc/make.bat include test/Makefile test/*.sh test/files/*.rar test/files/*.exp rarfile-rarfile_2_7/Makefile000066400000000000000000000007611243431105200162320ustar00rootroot00000000000000 prefix = /usr/local web = mkz@shell.berlios.de:/home/groups/rarfile/htdocs all: python setup.py build install: python setup.py install --prefix=$(prefix) tgz: clean python setup.py sdist clean: rm -rf __pycache__ build dist rm -f *.pyc MANIFEST *.orig *.rej *.html *.class rm -rf doc/_build doc/_static doc/_templates make -C test clean html: rst2html README.rst > README.html make -C doc html lint: pylint -E rarfile.py rbuild: curl -X POST http://readthedocs.org/build/6715 rarfile-rarfile_2_7/README.rst000066400000000000000000000016371243431105200162640ustar00rootroot00000000000000 rarfile - RAR archive reader for Python ======================================= This is Python module for RAR_ archive reading. The interface is made as zipfile_ like as possible. Licensed under ISC_ license. .. _RAR: http://en.wikipedia.org/wiki/RAR .. _zipfile: http://docs.python.org/library/zipfile.html .. _ISC: http://en.wikipedia.org/wiki/ISC_license Features: - Supports both RAR 2.x and 3.x archives. - Supports multi volume archives. - Supports Unicode filenames. - Supports password-protected archives. - Supports archive and file comments. - Archive parsing and non-compressed files are handled in pure Python code. - For compressed files runs ``unrar`` utility. - Works with both Python 2.x and 3.x. Links: - `Documentation`_ - `Downloads`_ - `Git`_ repo .. _Git: https://github.com/markokr/rarfile .. _Downloads: https://pypi.python.org/pypi/rarfile .. _Documentation: https://rarfile.readthedocs.org/ rarfile-rarfile_2_7/doc/000077500000000000000000000000001243431105200153335ustar00rootroot00000000000000rarfile-rarfile_2_7/doc/Makefile000066400000000000000000000127001243431105200167730ustar00rootroot00000000000000# Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: -rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/RarFile.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/RarFile.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/RarFile" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/RarFile" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." rarfile-rarfile_2_7/doc/api.rst000066400000000000000000000036061243431105200166430ustar00rootroot00000000000000 rarfile API documentation ========================= .. contents:: Table Of Contents Introduction ------------ .. automodule:: rarfile RarFile class ------------- .. autoclass:: RarFile :members: :inherited-members: RarInfo class ------------- .. autoclass:: RarInfo :members: :inherited-members: RarExtFile class ---------------- .. autoclass:: RarExtFile :members: :inherited-members: Functions --------- .. autofunction:: is_rarfile Module Configuration -------------------- .. autodata:: UNRAR_TOOL .. autodata:: DEFAULT_CHARSET .. autodata:: TRY_ENCODINGS .. autodata:: USE_DATETIME .. autodata:: PATH_SEP .. autodata:: NEED_COMMENTS .. autodata:: UNICODE_COMMENTS .. autodata:: USE_EXTRACT_HACK .. autodata:: HACK_SIZE_LIMIT Constants --------- .. py:data:: RAR_M0 No compression. .. py:data:: RAR_M1 Compression level `-m1` - Fastest compression. .. py:data:: RAR_M2 Compression level `-m2`. .. py:data:: RAR_M3 Compression level `-m3`. .. py:data:: RAR_M4 Compression level `-m4`. .. py:data:: RAR_M5 Compression level `-m5` - Maximum compression. .. py:data:: RAR_OS_MSDOS .. py:data:: RAR_OS_OS2 .. py:data:: RAR_OS_WIN32 .. py:data:: RAR_OS_UNIX .. py:data:: RAR_OS_MACOS .. py:data:: RAR_OS_BEOS Exceptions ---------- .. autoclass:: Error .. autoclass:: BadRarFile .. autoclass:: NotRarFile .. autoclass:: BadRarName .. autoclass:: NoRarEntry .. autoclass:: PasswordRequired .. autoclass:: NeedFirstVolume .. autoclass:: NoCrypto .. autoclass:: RarExecError .. autoclass:: RarWarning .. autoclass:: RarFatalError .. autoclass:: RarCRCError .. autoclass:: RarLockedArchiveError .. autoclass:: RarWriteError .. autoclass:: RarOpenError .. autoclass:: RarUserError .. autoclass:: RarMemoryError .. autoclass:: RarCreateError .. autoclass:: RarNoFilesError .. autoclass:: RarUserBreak .. autoclass:: RarUnknownError .. autoclass:: RarSignalExit rarfile-rarfile_2_7/doc/conf.py000066400000000000000000000175341243431105200166440ustar00rootroot00000000000000# -*- coding: utf-8 -*- # # RarFile documentation build configuration file, created by # sphinx-quickstart on Sun Mar 24 13:29:46 2013. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os, os.path # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import rarfile # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx'] autodoc_member_order = 'bysource' autoclass_content = 'both' autodoc_default_flags = ['show-inheritance'] intersphinx_mapping = {'python': ('http://docs.python.org/2', None)} # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'RarFile' copyright = u'2005-2013, Marko Kreen' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = rarfile.__version__ # The full version, including alpha/beta/rc tags. release = rarfile.__version__ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'default' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". #html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. html_show_sphinx = False # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. #htmlhelp_basename = 'RarFiledoc' # -- Options for LaTeX output -------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ('index', 'RarFile.tex', u'RarFile Documentation', u'Marko Kreen', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). #man_pages = [ # ('index', 'rarfile', u'RarFile Documentation', # [u'Marko Kreen'], 1) #] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'RarFile', u'RarFile Documentation', u'Marko Kreen', 'RarFile', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' rarfile-rarfile_2_7/doc/faq.rst000066400000000000000000000054701243431105200166420ustar00rootroot00000000000000 rarfile FAQ =========== .. contents:: Table of Contents What are the dependencies? -------------------------- It depends on ``unrar`` command-line utility to do the actual decompression. Note that by default it expect it to be in ``PATH``. If unrar launching fails, you need to fix this. Does it parse ``unrar`` output to get archive contents? ------------------------------------------------------- No, :mod:`rarfile` parses RAR structure in Python code. Also it can read uncompressed files from archive without external utility. Will rarfile support wrapping unrarlib/unrar.dll/unrar.so in the future? ------------------------------------------------------------------------ No. The current architecture - parsing in Python and decompression with command line tools work well across all interesting operating systems (Windows/Linux/MacOS), wrapping a library does not bring any advantages. Simple execution of command-line tools is also legally simpler situation than linking with external library. How can I get it work on Windows? --------------------------------- On Windows the ``unrar.exe`` is not in ``PATH`` so simple ``Popen("unrar ..")`` does not work. It can be solved several ways: 1. Add location of ``unrar.exe`` to PATH. 2. Set :data:`rarfile.UNRAR_TOOL` to full path of ``unrar.exe``. 3. Copy ``unrar.exe`` to your program directory. 4. Copy ``unrar.exe`` to system directory that is in PATH, eg. ``C:\Windows``. How to avoid the need for user to manually install rarfile/unrar? ----------------------------------------------------------------- Include ``rarfile.py`` and/or ``unrar`` with your application. Will it support creating RAR archives? -------------------------------------- No. RARLAB_ is not interested in RAR becoming open format and specifically discourages writing RAR creation software. In the meantime use either Zip_ (better compatibility) or 7z_ (better compression) format for your own archives. .. _RARLAB: http://www.rarlab.com/ .. _Zip: http://en.wikipedia.org/wiki/ZIP_%28file_format%29 .. _7z: http://en.wikipedia.org/wiki/7z What is the USE_EXTRACT_HACK? ----------------------------- RarFile uses ``unrar`` to extract compressed files. But when extracting single file from archive containing many entries, ``unrar`` needs to parse whole archive until it finds the right entry. This makes random-access to entries slow. To avoid that, RarFile remembers location of compressed data for each entry and on read it copies it to temporary archive containing only data for that one file, thus making ``unrar`` fast. The logic is only activated for entries smaller than :data:`rarfile.HACK_SIZE_LIMIT` (20M by default). Bigger files are accessed directly from RAR. Note - it only works for non-solid archives. So if you care about random access to files in your archive, do not create solid archives. rarfile-rarfile_2_7/doc/index.rst000066400000000000000000000016431243431105200172000ustar00rootroot00000000000000 rarfile - RAR archive reader for Python ======================================= This is Python module for RAR_ archive reading. The interface is made as zipfile_ like as possible. Licensed under ISC_ license. .. _RAR: http://en.wikipedia.org/wiki/RAR .. _zipfile: http://docs.python.org/library/zipfile.html .. _ISC: http://en.wikipedia.org/wiki/ISC_license Features: - Supports both RAR 2.x and 3.x archives. - Supports multi volume archives. - Supports Unicode filenames. - Supports password-protected archives. - Supports archive and file comments. - Archive parsing and non-compressed files are handled in pure Python code. - For compressed files runs ``unrar`` utility. - Works with both Python 2.x and 3.x. Documentation: .. toctree:: :maxdepth: 1 Module Documentation FAQs Release News Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` rarfile-rarfile_2_7/doc/make.bat000066400000000000000000000117521243431105200167460ustar00rootroot00000000000000@ECHO OFF REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=_build set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% ) if "%1" == "" goto help if "%1" == "help" ( :help echo.Please use `make ^` where ^ is one of echo. html to make standalone HTML files echo. dirhtml to make HTML files named index.html in directories echo. singlehtml to make a single large HTML file echo. pickle to make pickle files echo. json to make JSON files echo. htmlhelp to make HTML files and a HTML help project echo. qthelp to make HTML files and a qthelp project echo. devhelp to make HTML files and a Devhelp project echo. epub to make an epub echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter echo. text to make text files echo. man to make manual pages echo. texinfo to make Texinfo files echo. gettext to make PO message catalogs echo. changes to make an overview over all changed/added/deprecated items echo. linkcheck to check all external links for integrity echo. doctest to run all doctests embedded in the documentation if enabled goto end ) if "%1" == "clean" ( for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i del /q /s %BUILDDIR%\* goto end ) if "%1" == "html" ( %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/html. goto end ) if "%1" == "dirhtml" ( %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. goto end ) if "%1" == "singlehtml" ( %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. goto end ) if "%1" == "pickle" ( %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the pickle files. goto end ) if "%1" == "json" ( %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the JSON files. goto end ) if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. goto end ) if "%1" == "qthelp" ( %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run "qcollectiongenerator" with the ^ .qhcp project file in %BUILDDIR%/qthelp, like this: echo.^> qcollectiongenerator %BUILDDIR%\qthelp\RarFile.qhcp echo.To view the help file: echo.^> assistant -collectionFile %BUILDDIR%\qthelp\RarFile.ghc goto end ) if "%1" == "devhelp" ( %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp if errorlevel 1 exit /b 1 echo. echo.Build finished. goto end ) if "%1" == "epub" ( %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub if errorlevel 1 exit /b 1 echo. echo.Build finished. The epub file is in %BUILDDIR%/epub. goto end ) if "%1" == "latex" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex if errorlevel 1 exit /b 1 echo. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. goto end ) if "%1" == "text" ( %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text if errorlevel 1 exit /b 1 echo. echo.Build finished. The text files are in %BUILDDIR%/text. goto end ) if "%1" == "man" ( %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man if errorlevel 1 exit /b 1 echo. echo.Build finished. The manual pages are in %BUILDDIR%/man. goto end ) if "%1" == "texinfo" ( %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo if errorlevel 1 exit /b 1 echo. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. goto end ) if "%1" == "gettext" ( %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale if errorlevel 1 exit /b 1 echo. echo.Build finished. The message catalogs are in %BUILDDIR%/locale. goto end ) if "%1" == "changes" ( %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes if errorlevel 1 exit /b 1 echo. echo.The overview file is in %BUILDDIR%/changes. goto end ) if "%1" == "linkcheck" ( %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck if errorlevel 1 exit /b 1 echo. echo.Link check complete; look for any errors in the above output ^ or in %BUILDDIR%/linkcheck/output.txt. goto end ) if "%1" == "doctest" ( %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest if errorlevel 1 exit /b 1 echo. echo.Testing of doctests in the sources finished, look at the ^ results in %BUILDDIR%/doctest/output.txt. goto end ) :end rarfile-rarfile_2_7/doc/news.rst000066400000000000000000000167651243431105200170600ustar00rootroot00000000000000 rarfile history =============== .. py:currentmodule:: rarfile Version 2.7 (2014-11-23) ------------------------ * Allow use of bsdtar_ as decompression backend. It sits on top of libarchive_, which has support for reading RAR archives. Limitations of ``libarchive`` RAR backend: - Does not support solid archives. - Does not support password-protected archives. - Does not support "parsing filters" used for audio/image/executable data, so few non-solid, non-encrypted archives also fail. Now :mod:`rarfile` checks if ``unrar`` and if not then tries ``bsdtar``. If that works, then keeps using it. If not then configuration stays with ``unrar`` which will then appear in error messages. .. _bsdtar: https://github.com/libarchive/libarchive/wiki/ManPageBsdtar1 .. _libarchive: http://www.libarchive.org/ * Both :class:`RarFile` and :func:`is_rarfile` now accept file-like object. Eg. :class:`io.BytesIO`. Only requirement is that the object must be seekable. This mirrors similar funtionality in zipfile. Based on patch by Chase Zhang. * Uniform error handling. :class:`RarFile` accepts ``errors="strict"`` argument. Allow user to tune whether parsing and missing file errors will raise exception. If error is not raised, the error string can be queried with :meth:`RarFile.strerror` method. Version 2.6 (2013-04-10) ------------------------ * Add context manager support for :class:`RarFile` class. Both :class:`RarFile` and :class:`RarExtFile` support :keyword:`with` statement now. (Wentao Han) * :meth:`RarFile.volumelist` method, returns filenames of archive volumes. * Re-throw clearer error in case ``unrar`` is not found in ``PATH``. * Sync new unrar4.x error code from ``rar.txt``. * Use Sphinx for documentation, push docs to rtfd.org_ .. _rtfd.org: https://rarfile.readthedocs.org/ Version 2.5 (2012-01-19) ------------------------ Fixes: * :meth:`RarExtFile.read` and :meth:`RarExtFile.readinto` now do looping read to work properly on short reads. Important for Python 3.2+ where read from pipe can return short result even on blocking file descriptor. * Proper error reporting in :meth:`RarFile.extract`, :meth:`RarFile.extractall` and :meth:`RarFile.testrar`. * :meth:`RarExtFile.read` from unrar pipe: prefer to return unrar error code, if thats not available, do own error checks. * Avoid string addition in :meth:`RarExtFile.read`, instead use always list+join to merge multi-part reads. * dumprar: dont re-encode byte strings (Python 2.x). This avoids unneccessary failure when printing invalid unicode. Version 2.4 (2011-11-05) ------------------------ Fixes: * :data:`USE_DATETIME`: survive bad values from RAR * Fix bug in corrupt unicode filename handling * dumprar: make unicode chars work with both pipe and console Version 2.3 (2011-07-03) ------------------------ Features: * Support .seek() method on file streams. (Kristian Larsson) * Support .readinto() method on file streams. Optimized implementation is available on Python 2.6+ where :class:`memoryview` is available. * Support file comments - :attr:`RarInfo.comment` contains decompressed data if available. * File objects returned by :meth:`RarFile.open()` are :class:`io.RawIOBase`-compatible. They can further wrapped with :class:`io.BufferedReader` and :class:`io.TextIOWrapper`. * Now .getinfo() uses dict lookup instead of sequential scan when searching archive entry. This speeds up prococessing for archives that have many entries. * Option :data:`UNICODE_COMMENTS` to decode both archive and file comments to unicode. It uses :data:`TRY_ENCODINGS` for list of encodings to try. If off, comments are left as byte strings. Default: 0 * Option :data:`PATH_SEP` to change path separator. Default: ``r'\'``, set ``rarfile.PATH_SEP='/'`` to be compatibe with zipfile. * Option :data:`USE_DATETIME` to convert timestamps to datetime objects. Default: 0, timestamps are tuples. * Option :data:`TRY_ENCODINGS` to allow tuning attempted encoding list. * Reorder :class:`RarInfo` fiels to better show zipfile-compatible fields. * Standard regtests to make sure various features work Compatibility: * Drop :attr:`RarInfo.unicode_filename`, plain :attr:`RarInfo.filename` is already unicode since 2.0. * .read(-1) reads now until EOF. Previously it returned empty buffer. Fixes: * Make encrypted headers work with Python 3.x bytes() and with old 2.x 'sha' module. * Simplify :class:`subprocess.Popen` usage when launching ``unrar``. Previously it tried to optimize and work around OS/Python bugs, but this is not maintainable. * Use temp rar file hack on multi-volume archives too. * Always .wait() on unrar, to avoid zombies * Convert struct.error to BadRarFile * Plug some fd leaks. Affected: Jython, PyPy. * Broken archives are handled more robustly. Version 2.2 (2010-08-19) ------------------------ Fixes: * Relaxed volume naming. Now it just calculates new volume name by finding number in old one and increasing it, without any expectations what that number should be. * Files with 4G of compressed data in one colume were handled wrong. Fix. * DOS timestamp seconds need to be multiplied with 2. * Correct EXTTIME parsing. Cleanups: * Compressed size is per-volume, sum them together, so that user sees complete compressed size for files split over several volumes. * dumprar: Show unknown bits. * Use :class:`struct.Struct` to cache unpack formats. * Support missing :data:`os.devnull`. (Python 2.3) Version 2.1 (2010-07-31) ------------------------ Features: * Minimal implmentation for :meth:`RarFile.extract`, :meth:`RarFile.extractall`, :meth:`RarFile.testrar`. They are simple shortcuts to ``unrar`` invocation. * Accept :class:`RarInfo` object where filename is expected. * Include ``dumprar.py`` in .tgz. It can be used to visualize RAR structure and test module. * Support for encrypted file headers. Fixes: * Don't read past ENDARC, there could be non-RAR data there. * RAR 2.x: It does not write ENDARC, but our volume code expected it. Fix that. * RAR 2.x: Support more than 200 old-style volumes. Cleanups: * Load comment only when requested. * Cleanup of internal config variables. They should have now final names. * :meth:`RarFile.open`: Add mode=r argument to match zipfile. * Doc and comments cleanup, minimize duplication. * Common wrappers for both compressed and uncompressed files, now :meth:`RarFile.open` also does CRC-checking. Version 2.0 (2010-04-29) ------------------------ Features: * Python 3 support. Still works with 2.x. * Parses extended time fields. (.mtime, .ctime, .atime) * :meth:`RarFile.open` method. This makes possible to process large entries that do not fit into memory. * Supports password-protected archives. * Supports archive comments. Cleanups: * Uses :mod:`subprocess` module to launch ``unrar``. * .filename is always Unicode string, .unicode_filename is now deprecated. * .CRC is unsigned again, as python3 crc32() is unsigned. Version 1.1 (2008-08-31) ------------------------ Fixes: * Replace :func:`os.tempnam` with :func:`tempfile.mkstemp`. (Jason Moiron) * Fix infinite loop in _extract_hack on unexpected EOF * :attr:`RarInfo.CRC` is now signed value to match crc32() * :meth:`RarFile.read` now checks file crc Cleanups: * more docstrings * throw proper exceptions (subclasses of :exc:`rarfile.Error`) * RarInfo has fields pre-initialized, so they appear in help() * rename RarInfo.data to RarInfo.header_data * dont use "print" when header parsing fails * use try/finally to delete temp rar Version 1.0 (2005-08-08) ------------------------ * First release. rarfile-rarfile_2_7/dumprar.py000077500000000000000000000223761243431105200166270ustar00rootroot00000000000000#! /usr/bin/env python """Dump archive contents, test extraction.""" import sys import rarfile as rf from binascii import crc32, hexlify from datetime import datetime try: bytearray except NameError: import array def bytearray(v): return array.array('B', v) rf.UNICODE_COMMENTS = 1 rf.USE_DATETIME = 1 usage = """ dumprar [switches] [ARC1 ARC2 ...] [@ARCLIST] switches: @file read archive names from file -pPSW set password -Ccharset set fallback charset -v increase verbosity -t attemt to read all files -x write read files out -c show archive comment -h show usage -- stop switch parsing """.strip() os_list = ['DOS', 'OS2', 'WIN', 'UNIX', 'MACOS', 'BEOS'] block_strs = ['MARK', 'MAIN', 'FILE', 'OLD_COMMENT', 'OLD_EXTRA', 'OLD_SUB', 'OLD_RECOVERY', 'OLD_AUTH', 'SUB', 'ENDARC'] def rarType(type): if type < rf.RAR_BLOCK_MARK or type > rf.RAR_BLOCK_ENDARC: return "*UNKNOWN*" return block_strs[type - rf.RAR_BLOCK_MARK] main_bits = ( (rf.RAR_MAIN_VOLUME, "VOL"), (rf.RAR_MAIN_COMMENT, "COMMENT"), (rf.RAR_MAIN_LOCK, "LOCK"), (rf.RAR_MAIN_SOLID, "SOLID"), (rf.RAR_MAIN_NEWNUMBERING, "NEWNR"), (rf.RAR_MAIN_AUTH, "AUTH"), (rf.RAR_MAIN_RECOVERY, "RECOVERY"), (rf.RAR_MAIN_PASSWORD, "PASSWORD"), (rf.RAR_MAIN_FIRSTVOLUME, "FIRSTVOL"), (rf.RAR_SKIP_IF_UNKNOWN, "SKIP"), (rf.RAR_LONG_BLOCK, "LONG"), ) endarc_bits = ( (rf.RAR_ENDARC_NEXT_VOLUME, "NEXTVOL"), (rf.RAR_ENDARC_DATACRC, "DATACRC"), (rf.RAR_ENDARC_REVSPACE, "REVSPACE"), (rf.RAR_ENDARC_VOLNR, "VOLNR"), (rf.RAR_SKIP_IF_UNKNOWN, "SKIP"), (rf.RAR_LONG_BLOCK, "LONG"), ) file_bits = ( (rf.RAR_FILE_SPLIT_BEFORE, "SPLIT_BEFORE"), (rf.RAR_FILE_SPLIT_AFTER, "SPLIT_AFTER"), (rf.RAR_FILE_PASSWORD, "PASSWORD"), (rf.RAR_FILE_COMMENT, "COMMENT"), (rf.RAR_FILE_SOLID, "SOLID"), (rf.RAR_FILE_LARGE, "LARGE"), (rf.RAR_FILE_UNICODE, "UNICODE"), (rf.RAR_FILE_SALT, "SALT"), (rf.RAR_FILE_VERSION, "VERSION"), (rf.RAR_FILE_EXTTIME, "EXTTIME"), (rf.RAR_FILE_EXTFLAGS, "EXTFLAGS"), (rf.RAR_SKIP_IF_UNKNOWN, "SKIP"), (rf.RAR_LONG_BLOCK, "LONG"), ) generic_bits = ( (rf.RAR_SKIP_IF_UNKNOWN, "SKIP"), (rf.RAR_LONG_BLOCK, "LONG"), ) file_parms = ("D64", "D128", "D256", "D512", "D1024", "D2048", "D4096", "DIR") def xprint(m, *args): if sys.hexversion < 0x3000000: m = m.decode('utf8') if args: m = m % args if sys.hexversion < 0x3000000: m = m.encode('utf8') sys.stdout.write(m) sys.stdout.write('\n') def render_flags(flags, bit_list): res = [] known = 0 for bit in bit_list: known = known | bit[0] if flags & bit[0]: res.append(bit[1]) unknown = flags & ~known n = 0 while unknown: if unknown & 1: res.append("UNK_%04x" % (1 << n)) unknown = unknown >> 1 n += 1 return ",".join(res) def get_file_flags(flags): res = render_flags(flags & ~rf.RAR_FILE_DICTMASK, file_bits) xf = (flags & rf.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): if isinstance(t, datetime): return t.isoformat(' ') return "%04d-%02d-%02d %02d:%02d:%02d" % t def show_item(h): st = rarType(h.type) unknown = h.header_size - h.header_base xprint("%s: hdrlen=%d datlen=%d hdr_unknown=%d", st, h.header_size, h.add_size, unknown) if unknown > 0 and cf_verbose > 1: dat = h.header_data[h.header_base : ] xprint(" unknown: %s", hexlify(dat)) if h.type in (rf.RAR_BLOCK_FILE, rf.RAR_BLOCK_SUB): if h.host_os == rf.RAR_OS_UNIX: s_mode = "0%o" % h.mode else: s_mode = "0x%x" % h.mode xprint(" flags=0x%04x:%s", h.flags, get_file_flags(h.flags)) if h.host_os >= 0 and h.host_os < len(os_list): s_os = os_list[h.host_os] else: s_os = "?" xprint(" os=%d:%s ver=%d mode=%s meth=%c cmp=%d dec=%d vol=%d", h.host_os, s_os, h.extract_version, s_mode, h.compress_type, h.compress_size, h.file_size, h.volume) ucrc = (h.CRC + (1 << 32)) & ((1 << 32) - 1) xprint(" crc=0x%08x (%d) time=%s", ucrc, h.CRC, fmt_time(h.date_time)) xprint(" name=%s", h.filename) if h.mtime: xprint(" mtime=%s", fmt_time(h.mtime)) if h.ctime: xprint(" ctime=%s", fmt_time(h.ctime)) if h.atime: xprint(" atime=%s", fmt_time(h.atime)) if h.arctime: xprint(" arctime=%s", fmt_time(h.arctime)) elif h.type == rf.RAR_BLOCK_MAIN: xprint(" flags=0x%04x:%s", h.flags, get_main_flags(h.flags)) elif h.type == rf.RAR_BLOCK_ENDARC: xprint(" flags=0x%04x:%s", h.flags, get_endarc_flags(h.flags)) elif h.type == rf.RAR_BLOCK_MARK: xprint(" flags=0x%04x:", h.flags) else: xprint(" flags=0x%04x:%s", h.flags, get_generic_flags(h.flags)) if h.comment is not None: cm = repr(h.comment) if cm[0] == 'u': cm = cm[1:] xprint(" comment=%s", cm) cf_show_comment = 0 cf_verbose = 0 cf_charset = None cf_extract = 0 cf_test_read = 0 cf_test_unrar = 0 def check_crc(f, inf): ucrc = f.CRC if ucrc < 0: ucrc += (long(1) << 32) if ucrc != inf.CRC: print ('crc error') def test_read_long(r, inf): f = r.open(inf.filename) total = 0 while 1: data = f.read(8192) if not data: break total += len(data) if total != inf.file_size: xprint("\n *** %s has corrupt file: %s ***", r.rarfile, inf.filename) xprint(" *** short read: got=%d, need=%d ***\n", total, inf.file_size) check_crc(f, inf) # test .seek() & .readinto() if cf_test_read > 1: f.seek(0,0) # hack: re-enable crc calc f.crc_check = 1 f.CRC = 0 total = 0 buf = bytearray(rf.ZERO*4096) while 1: res = f.readinto(buf) if not res: break total += res if inf.file_size != total: xprint(" *** readinto failed: got=%d, need=%d ***\n", total, inf.file_size) check_crc(f, inf) f.close() def test_read(r, inf): test_read_long(r, inf) def test_real(fn, psw): xprint("Archive: %s", fn) cb = None if cf_verbose > 1: cb = show_item # check if rar if not rf.is_rarfile(fn): xprint(" --- %s is not a RAR file ---", fn) return # open r = rf.RarFile(fn, charset = cf_charset, info_callback = cb) # set password if r.needs_password(): if psw: r.setpassword(psw) else: xprint(" --- %s requires password ---", fn) return # show comment if cf_show_comment and r.comment: for ln in r.comment.split('\n'): xprint(" %s", ln) elif cf_verbose == 1 and r.comment: cm = repr(r.comment) if cm[0] == 'u': cm = cm[1:] xprint(" comment=%s", cm) # process for n in r.namelist(): inf = r.getinfo(n) if inf.isdir(): continue if cf_verbose == 1: show_item(inf) if cf_test_read: test_read(r, inf) if cf_extract: r.extractall() for inf in r.infolist(): r.extract(inf) if cf_test_unrar: r.testrar() def test(fn, psw): try: test_real(fn, psw) except rf.NeedFirstVolume: xprint(" --- %s is middle part of multi-vol archive ---", fn) except rf.Error: exc, msg, tb = sys.exc_info() xprint("\n *** %s: %s ***\n", exc.__name__, msg) del tb except IOError: exc, msg, tb = sys.exc_info() xprint("\n *** %s: %s ***\n", exc.__name__, msg) del tb def main(): global cf_verbose, cf_show_comment, cf_charset global cf_extract, cf_test_read, cf_test_unrar # parse args args = [] psw = None noswitch = False for a in sys.argv[1:]: if noswitch: args.append(a) elif a[0] == "@": for ln in open(a[1:], 'r'): fn = ln[:-1] args.append(fn) elif a[0] != '-': args.append(a) elif a[1] == 'p': psw = a[2:] elif a == '--': noswitch = True elif a == '-h': xprint(usage) return elif a == '-v': cf_verbose += 1 elif a == '-c': cf_show_comment = 1 elif a == '-x': cf_extract = 1 elif a == '-t': cf_test_read += 1 elif a == '-T': cf_test_unrar = 1 elif a[1] == 'C': cf_charset = a[2:] else: raise Exception("unknown switch: "+a) if not args: xprint(usage) for fn in args: test(fn, psw) if __name__ == '__main__': try: main() except KeyboardInterrupt: pass rarfile-rarfile_2_7/rarfile.py000066400000000000000000001613141243431105200165720ustar00rootroot00000000000000# rarfile.py # # Copyright (c) 2005-2014 Marko Kreen # # Permission to use, copy, modify, and/or 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. r"""RAR archive reader. This is Python module for Rar archive reading. The interface is made as :mod:`zipfile`-like as possible. Basic logic: - Parse archive structure with Python. - Extract non-compressed files with Python - Extract compressed files with unrar. - Optionally write compressed data to temp file to speed up unrar, otherwise it needs to scan whole archive on each execution. Example:: import rarfile rf = rarfile.RarFile('myarchive.rar') for f in rf.infolist(): print f.filename, f.file_size if f.filename == 'README': print(rf.read(f)) Archive files can also be accessed via file-like object returned by :meth:`RarFile.open`:: import rarfile with rarfile.RarFile('archive.rar') as rf: with rf.open('README') as f: for ln in f: print(ln.strip()) There are few module-level parameters to tune behaviour, here they are with defaults, and reason to change it:: import rarfile # Set to full path of unrar.exe if it is not in PATH rarfile.UNRAR_TOOL = "unrar" # Set to 0 if you don't look at comments and want to # avoid wasting time for parsing them rarfile.NEED_COMMENTS = 1 # Set up to 1 if you don't want to deal with decoding comments # from unknown encoding. rarfile will try couple of common # encodings in sequence. rarfile.UNICODE_COMMENTS = 0 # Set to 1 if you prefer timestamps to be datetime objects # instead tuples rarfile.USE_DATETIME = 0 # Set to '/' to be more compatible with zipfile rarfile.PATH_SEP = '\\' For more details, refer to source. """ __version__ = '2.7' # export only interesting items __all__ = ['is_rarfile', 'RarInfo', 'RarFile', 'RarExtFile'] ## ## Imports and compat - support both Python 2.x and 3.x ## import sys, os, struct, errno from struct import pack, unpack from binascii import crc32 from tempfile import mkstemp from subprocess import Popen, PIPE, STDOUT from datetime import datetime # only needed for encryped headers try: from Crypto.Cipher import AES try: from hashlib import sha1 except ImportError: from sha import new as sha1 _have_crypto = 1 except ImportError: _have_crypto = 0 # compat with 2.x if sys.hexversion < 0x3000000: # prefer 3.x behaviour range = xrange # py2.6 has broken bytes() def bytes(s, enc): return str(s) else: unicode = str # see if compat bytearray() is needed try: bytearray except NameError: import array class bytearray: def __init__(self, val = ''): self.arr = array.array('B', val) self.append = self.arr.append self.__getitem__ = self.arr.__getitem__ self.__len__ = self.arr.__len__ def decode(self, *args): return self.arr.tostring().decode(*args) # Optimized .readinto() requires memoryview try: memoryview have_memoryview = 1 except NameError: have_memoryview = 0 # Struct() for older python try: from struct import Struct except ImportError: class Struct: def __init__(self, fmt): self.format = fmt self.size = struct.calcsize(fmt) def unpack(self, buf): return unpack(self.format, buf) def unpack_from(self, buf, ofs = 0): return unpack(self.format, buf[ofs : ofs + self.size]) def pack(self, *args): return pack(self.format, *args) # file object superclass try: from io import RawIOBase except ImportError: class RawIOBase(object): def close(self): pass ## ## Module configuration. Can be tuned after importing. ## #: default fallback charset DEFAULT_CHARSET = "windows-1252" #: list of encodings to try, with fallback to DEFAULT_CHARSET if none succeed TRY_ENCODINGS = ('utf8', 'utf-16le') #: 'unrar', 'rar' or full path to either one UNRAR_TOOL = "unrar" #: Command line args to use for opening file for reading. OPEN_ARGS = ('p', '-inul') #: Command line args to use for extracting file to disk. EXTRACT_ARGS = ('x', '-y', '-idq') #: args for testrar() TEST_ARGS = ('t', '-idq') # # Allow use of tool that is not compatible with unrar. # # By default use 'bsdtar' which is 'tar' program that # sits on top of libarchive. # # Problems with libarchive RAR backend: # - Does not support solid archives. # - Does not support password-protected archives. # ALT_TOOL = 'bsdtar' ALT_OPEN_ARGS = ('-x', '--to-stdout', '-f') ALT_EXTRACT_ARGS = ('-x', '-f') ALT_TEST_ARGS = ('-t', '-f') ALT_CHECK_ARGS = ('--help',) #: whether to speed up decompression by using tmp archive USE_EXTRACT_HACK = 1 #: limit the filesize for tmp archive usage HACK_SIZE_LIMIT = 20*1024*1024 #: whether to parse file/archive comments. NEED_COMMENTS = 1 #: whether to convert comments to unicode strings UNICODE_COMMENTS = 0 #: Convert RAR time tuple into datetime() object USE_DATETIME = 0 #: Separator for path name components. RAR internally uses '\\'. #: Use '/' to be similar with zipfile. PATH_SEP = '\\' ## ## rar constants ## # block types RAR_BLOCK_MARK = 0x72 # r RAR_BLOCK_MAIN = 0x73 # s RAR_BLOCK_FILE = 0x74 # t RAR_BLOCK_OLD_COMMENT = 0x75 # u RAR_BLOCK_OLD_EXTRA = 0x76 # v RAR_BLOCK_OLD_SUB = 0x77 # w RAR_BLOCK_OLD_RECOVERY = 0x78 # x RAR_BLOCK_OLD_AUTH = 0x79 # y RAR_BLOCK_SUB = 0x7a # z RAR_BLOCK_ENDARC = 0x7b # { # flags for RAR_BLOCK_MAIN RAR_MAIN_VOLUME = 0x0001 RAR_MAIN_COMMENT = 0x0002 RAR_MAIN_LOCK = 0x0004 RAR_MAIN_SOLID = 0x0008 RAR_MAIN_NEWNUMBERING = 0x0010 RAR_MAIN_AUTH = 0x0020 RAR_MAIN_RECOVERY = 0x0040 RAR_MAIN_PASSWORD = 0x0080 RAR_MAIN_FIRSTVOLUME = 0x0100 RAR_MAIN_ENCRYPTVER = 0x0200 # flags for RAR_BLOCK_FILE RAR_FILE_SPLIT_BEFORE = 0x0001 RAR_FILE_SPLIT_AFTER = 0x0002 RAR_FILE_PASSWORD = 0x0004 RAR_FILE_COMMENT = 0x0008 RAR_FILE_SOLID = 0x0010 RAR_FILE_DICTMASK = 0x00e0 RAR_FILE_DICT64 = 0x0000 RAR_FILE_DICT128 = 0x0020 RAR_FILE_DICT256 = 0x0040 RAR_FILE_DICT512 = 0x0060 RAR_FILE_DICT1024 = 0x0080 RAR_FILE_DICT2048 = 0x00a0 RAR_FILE_DICT4096 = 0x00c0 RAR_FILE_DIRECTORY = 0x00e0 RAR_FILE_LARGE = 0x0100 RAR_FILE_UNICODE = 0x0200 RAR_FILE_SALT = 0x0400 RAR_FILE_VERSION = 0x0800 RAR_FILE_EXTTIME = 0x1000 RAR_FILE_EXTFLAGS = 0x2000 # flags for RAR_BLOCK_ENDARC RAR_ENDARC_NEXT_VOLUME = 0x0001 RAR_ENDARC_DATACRC = 0x0002 RAR_ENDARC_REVSPACE = 0x0004 RAR_ENDARC_VOLNR = 0x0008 # flags common to all blocks RAR_SKIP_IF_UNKNOWN = 0x4000 RAR_LONG_BLOCK = 0x8000 # Host OS types RAR_OS_MSDOS = 0 RAR_OS_OS2 = 1 RAR_OS_WIN32 = 2 RAR_OS_UNIX = 3 RAR_OS_MACOS = 4 RAR_OS_BEOS = 5 # Compression methods - '0'..'5' RAR_M0 = 0x30 RAR_M1 = 0x31 RAR_M2 = 0x32 RAR_M3 = 0x33 RAR_M4 = 0x34 RAR_M5 = 0x35 ## ## internal constants ## RAR_ID = bytes("Rar!\x1a\x07\x00", 'ascii') ZERO = bytes("\0", 'ascii') EMPTY = bytes("", 'ascii') S_BLK_HDR = Struct(' HACK_SIZE_LIMIT: use_hack = 0 elif not USE_EXTRACT_HACK: use_hack = 0 # now extract if inf.compress_type == RAR_M0 and (inf.flags & RAR_FILE_PASSWORD) == 0: return self._open_clear(inf) elif use_hack: return self._open_hack(inf, psw) else: return self._open_unrar(self.rarfile, inf, psw) def read(self, fname, psw = None): """Return uncompressed data for archive entry. For longer files using :meth:`RarFile.open` may be better idea. Parameters: fname filename or RarInfo instance psw password to use for extracting. """ f = self.open(fname, 'r', psw) try: return f.read() finally: f.close() def close(self): """Release open resources.""" pass def printdir(self): """Print archive file list to stdout.""" for f in self._info_list: print(f.filename) def extract(self, member, path=None, pwd=None): """Extract single file into current directory. Parameters: member filename or :class:`RarInfo` instance path optional destination path pwd optional password to use """ if isinstance(member, RarInfo): fname = member.filename else: fname = member self._extract([fname], path, pwd) def extractall(self, path=None, members=None, pwd=None): """Extract all files into current directory. Parameters: path optional destination path members optional filename or :class:`RarInfo` instance list to extract pwd optional password to use """ fnlist = [] if members is not None: for m in members: if isinstance(m, RarInfo): fnlist.append(m.filename) else: fnlist.append(m) self._extract(fnlist, path, pwd) def testrar(self): """Let 'unrar' test the archive. """ cmd = [UNRAR_TOOL] + list(TEST_ARGS) add_password_arg(cmd, self._password) cmd.append(self.rarfile) p = custom_popen(cmd) output = p.communicate()[0] check_returncode(p, output) def strerror(self): """Return error string if parsing failed, or None if no problems. """ return self._parse_error ## ## private methods ## def _set_error(self, msg, *args): if args: msg = msg % args self._parse_error = msg if self._strict: raise BadRarFile(msg) # store entry def _process_entry(self, item): if item.type == RAR_BLOCK_FILE: # use only first part if (item.flags & RAR_FILE_SPLIT_BEFORE) == 0: self._info_map[item.filename] = item self._info_list.append(item) # remember if any items require password if item.needs_password(): self._needs_password = True elif len(self._info_list) > 0: # final crc is in last block old = self._info_list[-1] old.CRC = item.CRC old.compress_size += item.compress_size # parse new-style comment if item.type == RAR_BLOCK_SUB and item.filename == 'CMT': if not NEED_COMMENTS: pass elif item.flags & (RAR_FILE_SPLIT_BEFORE | RAR_FILE_SPLIT_AFTER): pass elif item.flags & RAR_FILE_SOLID: # file comment cmt = self._read_comment_v3(item, self._password) if len(self._info_list) > 0: old = self._info_list[-1] old.comment = cmt else: # archive comment cmt = self._read_comment_v3(item, self._password) self.comment = cmt if self._info_callback: self._info_callback(item) # read rar def _parse(self): self._fd = None try: self._parse_real() finally: if self._fd: self._fd.close() self._fd = None def _parse_real(self): fd = XFile(self.rarfile) self._fd = fd id = fd.read(len(RAR_ID)) if id != RAR_ID: raise NotRarFile("Not a Rar archive: "+self.rarfile) volume = 0 # first vol (.rar) is 0 more_vols = 0 endarc = 0 volfile = self.rarfile self._vol_list = [self.rarfile] while 1: if endarc: h = None # don't read past ENDARC else: h = self._parse_header(fd) if not h: if more_vols: volume += 1 fd.close() try: volfile = self._next_volname(volfile) fd = XFile(volfile) except IOError: self._set_error("Cannot open next volume: %s", volfile) break self._fd = fd more_vols = 0 endarc = 0 self._vol_list.append(volfile) continue break h.volume = volume h.volume_file = volfile if h.type == RAR_BLOCK_MAIN and not self._main: self._main = h if h.flags & RAR_MAIN_NEWNUMBERING: # RAR 2.x does not set FIRSTVOLUME, # so check it only if NEWNUMBERING is used if (h.flags & RAR_MAIN_FIRSTVOLUME) == 0: raise NeedFirstVolume("Need to start from first volume") if h.flags & RAR_MAIN_PASSWORD: self._needs_password = True if not self._password: self._main = None break elif h.type == RAR_BLOCK_ENDARC: more_vols = h.flags & RAR_ENDARC_NEXT_VOLUME endarc = 1 elif h.type == RAR_BLOCK_FILE: # RAR 2.x does not write RAR_BLOCK_ENDARC if h.flags & RAR_FILE_SPLIT_AFTER: more_vols = 1 # RAR 2.x does not set RAR_MAIN_FIRSTVOLUME if volume == 0 and h.flags & RAR_FILE_SPLIT_BEFORE: raise NeedFirstVolume("Need to start from first volume") # store it self._process_entry(h) # go to next header if h.add_size > 0: fd.seek(h.file_offset + h.add_size, 0) # AES encrypted headers _last_aes_key = (None, None, None) # (salt, key, iv) def _decrypt_header(self, fd): if not _have_crypto: raise NoCrypto('Cannot parse encrypted headers - no crypto') salt = fd.read(8) if self._last_aes_key[0] == salt: key, iv = self._last_aes_key[1:] else: key, iv = rar3_s2k(self._password, salt) self._last_aes_key = (salt, key, iv) return HeaderDecrypt(fd, key, iv) # read single header def _parse_header(self, fd): try: # handle encrypted headers if self._main and self._main.flags & RAR_MAIN_PASSWORD: if not self._password: return fd = self._decrypt_header(fd) # now read actual header return self._parse_block_header(fd) except struct.error: self._set_error('Broken header in RAR file') return None # common header def _parse_block_header(self, fd): h = RarInfo() h.header_offset = fd.tell() h.comment = None # read and parse base header buf = fd.read(S_BLK_HDR.size) if not buf: return None t = S_BLK_HDR.unpack_from(buf) h.header_crc, h.type, h.flags, h.header_size = t h.header_base = S_BLK_HDR.size pos = S_BLK_HDR.size # read full header if h.header_size > S_BLK_HDR.size: h.header_data = buf + fd.read(h.header_size - S_BLK_HDR.size) else: h.header_data = buf h.file_offset = fd.tell() # unexpected EOF? if len(h.header_data) != h.header_size: self._set_error('Unexpected EOF when reading header') return None # block has data assiciated with it? if h.flags & RAR_LONG_BLOCK: h.add_size = S_LONG.unpack_from(h.header_data, pos)[0] else: h.add_size = 0 # parse interesting ones, decide header boundaries for crc if h.type == RAR_BLOCK_MARK: return h elif h.type == RAR_BLOCK_MAIN: h.header_base += 6 if h.flags & RAR_MAIN_ENCRYPTVER: h.header_base += 1 if h.flags & RAR_MAIN_COMMENT: self._parse_subblocks(h, h.header_base) self.comment = h.comment elif h.type == RAR_BLOCK_FILE: self._parse_file_header(h, pos) elif h.type == RAR_BLOCK_SUB: self._parse_file_header(h, pos) h.header_base = h.header_size elif h.type == RAR_BLOCK_OLD_AUTH: h.header_base += 8 elif h.type == RAR_BLOCK_OLD_EXTRA: h.header_base += 7 else: h.header_base = h.header_size # check crc if h.type == RAR_BLOCK_OLD_SUB: crcdat = h.header_data[2:] + fd.read(h.add_size) else: crcdat = h.header_data[2:h.header_base] calc_crc = crc32(crcdat) & 0xFFFF # return good header if h.header_crc == calc_crc: return h # header parsing failed. self._set_error('Header CRC error (%02x): exp=%x got=%x (xlen = %d)', h.type, h.header_crc, calc_crc, len(crcdat)) # instead panicing, send eof return None # read file-specific header def _parse_file_header(self, h, pos): fld = S_FILE_HDR.unpack_from(h.header_data, pos) h.compress_size = fld[0] h.file_size = fld[1] h.host_os = fld[2] h.CRC = fld[3] h.date_time = parse_dos_time(fld[4]) h.extract_version = fld[5] h.compress_type = fld[6] h.name_size = fld[7] h.mode = fld[8] pos += S_FILE_HDR.size if h.flags & RAR_FILE_LARGE: h1 = S_LONG.unpack_from(h.header_data, pos)[0] h2 = S_LONG.unpack_from(h.header_data, pos + 4)[0] h.compress_size |= h1 << 32 h.file_size |= h2 << 32 pos += 8 h.add_size = h.compress_size name = h.header_data[pos : pos + h.name_size ] pos += h.name_size if h.flags & RAR_FILE_UNICODE: nul = name.find(ZERO) h.orig_filename = name[:nul] u = UnicodeFilename(h.orig_filename, name[nul + 1 : ]) h.filename = u.decode() # if parsing failed fall back to simple name if u.failed: h.filename = self._decode(h.orig_filename) else: h.orig_filename = name h.filename = self._decode(name) # change separator, if requested if PATH_SEP != '\\': h.filename = h.filename.replace('\\', PATH_SEP) if h.flags & RAR_FILE_SALT: h.salt = h.header_data[pos : pos + 8] pos += 8 else: h.salt = None # optional extended time stamps if h.flags & RAR_FILE_EXTTIME: pos = self._parse_ext_time(h, pos) else: h.mtime = h.atime = h.ctime = h.arctime = None # base header end h.header_base = pos if h.flags & RAR_FILE_COMMENT: self._parse_subblocks(h, pos) # convert timestamps if USE_DATETIME: h.date_time = to_datetime(h.date_time) h.mtime = to_datetime(h.mtime) h.atime = to_datetime(h.atime) h.ctime = to_datetime(h.ctime) h.arctime = to_datetime(h.arctime) # .mtime is .date_time with more precision if h.mtime: if USE_DATETIME: h.date_time = h.mtime else: # keep seconds int h.date_time = h.mtime[:5] + (int(h.mtime[5]),) return pos # find old-style comment subblock def _parse_subblocks(self, h, pos): hdata = h.header_data while pos < len(hdata): # ordinary block header t = S_BLK_HDR.unpack_from(hdata, pos) scrc, stype, sflags, slen = t pos_next = pos + slen pos += S_BLK_HDR.size # corrupt header if pos_next < pos: break # followed by block-specific header if stype == RAR_BLOCK_OLD_COMMENT and pos + S_COMMENT_HDR.size <= pos_next: declen, ver, meth, crc = S_COMMENT_HDR.unpack_from(hdata, pos) pos += S_COMMENT_HDR.size data = hdata[pos : pos_next] cmt = rar_decompress(ver, meth, data, declen, sflags, crc, self._password) if not self._crc_check: h.comment = self._decode_comment(cmt) elif crc32(cmt) & 0xFFFF == crc: h.comment = self._decode_comment(cmt) pos = pos_next def _parse_ext_time(self, h, pos): data = h.header_data # flags and rest of data can be missing flags = 0 if pos + 2 <= len(data): flags = S_SHORT.unpack_from(data, pos)[0] pos += 2 h.mtime, pos = self._parse_xtime(flags >> 3*4, data, pos, h.date_time) h.ctime, pos = self._parse_xtime(flags >> 2*4, data, pos) h.atime, pos = self._parse_xtime(flags >> 1*4, data, pos) h.arctime, pos = self._parse_xtime(flags >> 0*4, data, pos) return pos def _parse_xtime(self, flag, data, pos, dostime = None): unit = 10000000.0 # 100 ns units if flag & 8: if not dostime: t = S_LONG.unpack_from(data, pos)[0] dostime = parse_dos_time(t) pos += 4 rem = 0 cnt = flag & 3 for i in range(cnt): b = S_BYTE.unpack_from(data, pos)[0] rem = (b << 16) | (rem >> 8) pos += 1 sec = dostime[5] + rem / unit if flag & 4: sec += 1 dostime = dostime[:5] + (sec,) return dostime, pos # given current vol name, construct next one def _next_volname(self, volfile): if is_filelike(volfile): raise IOError("Working on single FD") if self._main.flags & RAR_MAIN_NEWNUMBERING: return self._next_newvol(volfile) return self._next_oldvol(volfile) # new-style next volume def _next_newvol(self, volfile): i = len(volfile) - 1 while i >= 0: if volfile[i] >= '0' and volfile[i] <= '9': return self._inc_volname(volfile, i) i -= 1 raise BadRarName("Cannot construct volume name: "+volfile) # old-style next volume def _next_oldvol(self, volfile): # rar -> r00 if volfile[-4:].lower() == '.rar': return volfile[:-2] + '00' return self._inc_volname(volfile, len(volfile) - 1) # increase digits with carry, otherwise just increment char def _inc_volname(self, volfile, i): fn = list(volfile) while i >= 0: if fn[i] != '9': fn[i] = chr(ord(fn[i]) + 1) break fn[i] = '0' i -= 1 return ''.join(fn) def _open_clear(self, inf): return DirectReader(self, inf) # put file compressed data into temporary .rar archive, and run # unrar on that, thus avoiding unrar going over whole archive def _open_hack(self, inf, psw = None): BSIZE = 32*1024 size = inf.compress_size + inf.header_size rf = XFile(inf.volume_file, 0) rf.seek(inf.header_offset) tmpfd, tmpname = mkstemp(suffix='.rar') tmpf = os.fdopen(tmpfd, "wb") try: # create main header: crc, type, flags, size, res1, res2 mh = S_BLK_HDR.pack(0x90CF, 0x73, 0, 13) + ZERO * (2+4) tmpf.write(RAR_ID + mh) while size > 0: if size > BSIZE: buf = rf.read(BSIZE) else: buf = rf.read(size) if not buf: raise BadRarFile('read failed: ' + inf.filename) tmpf.write(buf) size -= len(buf) tmpf.close() rf.close() except: rf.close() tmpf.close() os.unlink(tmpname) raise return self._open_unrar(tmpname, inf, psw, tmpname) def _read_comment_v3(self, inf, psw=None): # read data rf = XFile(inf.volume_file) rf.seek(inf.file_offset) data = rf.read(inf.compress_size) rf.close() # decompress cmt = rar_decompress(inf.extract_version, inf.compress_type, data, inf.file_size, inf.flags, inf.CRC, psw, inf.salt) # check crc if self._crc_check: crc = crc32(cmt) if crc < 0: crc += (long(1) << 32) if crc != inf.CRC: return None return self._decode_comment(cmt) # extract using unrar def _open_unrar(self, rarfile, inf, psw = None, tmpfile = None): if is_filelike(rarfile): raise ValueError("Cannot use unrar directly on memory buffer") cmd = [UNRAR_TOOL] + list(OPEN_ARGS) add_password_arg(cmd, psw) cmd.append(rarfile) # not giving filename avoids encoding related problems if not tmpfile: fn = inf.filename if PATH_SEP != os.sep: fn = fn.replace(PATH_SEP, os.sep) cmd.append(fn) # read from unrar pipe return PipeReader(self, inf, cmd, tmpfile) def _decode(self, val): for c in TRY_ENCODINGS: try: return val.decode(c) except UnicodeError: pass return val.decode(self._charset, 'replace') def _decode_comment(self, val): if UNICODE_COMMENTS: return self._decode(val) return val # call unrar to extract a file def _extract(self, fnlist, path=None, psw=None): cmd = [UNRAR_TOOL] + list(EXTRACT_ARGS) # pasoword psw = psw or self._password add_password_arg(cmd, psw) # rar file cmd.append(self.rarfile) # file list for fn in fnlist: if os.sep != PATH_SEP: fn = fn.replace(PATH_SEP, os.sep) cmd.append(fn) # destination path if path is not None: cmd.append(path + os.sep) # call p = custom_popen(cmd) output = p.communicate()[0] check_returncode(p, output) ## ## Utility classes ## class UnicodeFilename: """Handle unicode filename decompression""" def __init__(self, name, encdata): self.std_name = bytearray(name) self.encdata = bytearray(encdata) self.pos = self.encpos = 0 self.buf = bytearray() self.failed = 0 def enc_byte(self): try: c = self.encdata[self.encpos] self.encpos += 1 return c except IndexError: self.failed = 1 return 0 def std_byte(self): try: return self.std_name[self.pos] except IndexError: self.failed = 1 return ord('?') def put(self, lo, hi): self.buf.append(lo) self.buf.append(hi) self.pos += 1 def decode(self): hi = self.enc_byte() flagbits = 0 while self.encpos < len(self.encdata): if flagbits == 0: flags = self.enc_byte() flagbits = 8 flagbits -= 2 t = (flags >> flagbits) & 3 if t == 0: self.put(self.enc_byte(), 0) elif t == 1: self.put(self.enc_byte(), hi) elif t == 2: self.put(self.enc_byte(), self.enc_byte()) else: n = self.enc_byte() if n & 0x80: c = self.enc_byte() for i in range((n & 0x7f) + 2): lo = (self.std_byte() + c) & 0xFF self.put(lo, hi) else: for i in range(n + 2): self.put(self.std_byte(), 0) return self.buf.decode("utf-16le", "replace") class RarExtFile(RawIOBase): """Base class for file-like object that :meth:`RarFile.open` returns. Provides public methods and common crc checking. Behaviour: - no short reads - .read() and .readinfo() read as much as requested. - no internal buffer, use io.BufferedReader for that. If :mod:`io` module is available (Python 2.6+, 3.x), then this calls will inherit from :class:`io.RawIOBase` class. This makes line-based access available: :meth:`RarExtFile.readline` and ``for ln in f``. """ #: Filename of the archive entry name = None def __init__(self, rf, inf): RawIOBase.__init__(self) # standard io.* properties self.name = inf.filename self.mode = 'rb' self.rf = rf self.inf = inf self.crc_check = rf._crc_check self.fd = None self.CRC = 0 self.remain = 0 self.returncode = 0 self._open() def _open(self): if self.fd: self.fd.close() self.fd = None self.CRC = 0 self.remain = self.inf.file_size def read(self, cnt = None): """Read all or specified amount of data from archive entry.""" # sanitize cnt if cnt is None or cnt < 0: cnt = self.remain elif cnt > self.remain: cnt = self.remain if cnt == 0: return EMPTY # actual read data = self._read(cnt) if data: self.CRC = crc32(data, self.CRC) self.remain -= len(data) if len(data) != cnt: raise BadRarFile("Failed the read enough data") # done? if not data or self.remain == 0: #self.close() self._check() return data def _check(self): """Check final CRC.""" if not self.crc_check: return if self.returncode: check_returncode(self, '') if self.remain != 0: raise BadRarFile("Failed the read enough data") crc = self.CRC if crc < 0: crc += (long(1) << 32) if crc != self.inf.CRC: raise BadRarFile("Corrupt file - CRC check failed: " + self.inf.filename) def _read(self, cnt): """Actual read that gets sanitized cnt.""" def close(self): """Close open resources.""" RawIOBase.close(self) if self.fd: self.fd.close() self.fd = None def __del__(self): """Hook delete to make sure tempfile is removed.""" self.close() def readinto(self, buf): """Zero-copy read directly into buffer. Returns bytes read. """ data = self.read(len(buf)) n = len(data) try: buf[:n] = data except TypeError: import array if not isinstance(buf, array.array): raise buf[:n] = array.array(buf.typecode, data) return n def tell(self): """Return current reading position in uncompressed data.""" return self.inf.file_size - self.remain def seek(self, ofs, whence = 0): """Seek in data. On uncompressed files, the seeking works by actual seeks so it's fast. On compresses files its slow - forward seeking happends by reading ahead, backwards by re-opening and decompressing from the start. """ # disable crc check when seeking self.crc_check = 0 fsize = self.inf.file_size cur_ofs = self.tell() if whence == 0: # seek from beginning of file new_ofs = ofs elif whence == 1: # seek from current position new_ofs = cur_ofs + ofs elif whence == 2: # seek from end of file new_ofs = fsize + ofs else: raise ValueError('Invalid value for whence') # sanity check if new_ofs < 0: new_ofs = 0 elif new_ofs > fsize: new_ofs = fsize # do the actual seek if new_ofs >= cur_ofs: self._skip(new_ofs - cur_ofs) else: # process old data ? #self._skip(fsize - cur_ofs) # reopen and seek self._open() self._skip(new_ofs) return self.tell() def _skip(self, cnt): """Read and discard data""" while cnt > 0: if cnt > 8192: buf = self.read(8192) else: buf = self.read(cnt) if not buf: break cnt -= len(buf) def readable(self): """Returns True""" return True def writable(self): """Returns False. Writing is not supported.""" return False def seekable(self): """Returns True. Seeking is supported, although it's slow on compressed files. """ return True def readall(self): """Read all remaining data""" # avoid RawIOBase default impl return self.read() class PipeReader(RarExtFile): """Read data from pipe, handle tempfile cleanup.""" def __init__(self, rf, inf, cmd, tempfile=None): self.cmd = cmd self.proc = None self.tempfile = tempfile RarExtFile.__init__(self, rf, inf) def _close_proc(self): if not self.proc: return if self.proc.stdout: self.proc.stdout.close() if self.proc.stdin: self.proc.stdin.close() if self.proc.stderr: self.proc.stderr.close() self.proc.wait() self.returncode = self.proc.returncode self.proc = None def _open(self): RarExtFile._open(self) # stop old process self._close_proc() # launch new process self.returncode = 0 self.proc = custom_popen(self.cmd) self.fd = self.proc.stdout # avoid situation where unrar waits on stdin if self.proc.stdin: self.proc.stdin.close() def _read(self, cnt): """Read from pipe.""" # normal read is usually enough data = self.fd.read(cnt) if len(data) == cnt or not data: return data # short read, try looping buf = [data] cnt -= len(data) while cnt > 0: data = self.fd.read(cnt) if not data: break cnt -= len(data) buf.append(data) return EMPTY.join(buf) def close(self): """Close open resources.""" self._close_proc() RarExtFile.close(self) if self.tempfile: try: os.unlink(self.tempfile) except OSError: pass self.tempfile = None if have_memoryview: def readinto(self, buf): """Zero-copy read directly into buffer.""" cnt = len(buf) if cnt > self.remain: cnt = self.remain vbuf = memoryview(buf) res = got = 0 while got < cnt: res = self.fd.readinto(vbuf[got : cnt]) if not res: break if self.crc_check: self.CRC = crc32(vbuf[got : got + res], self.CRC) self.remain -= res got += res return got class DirectReader(RarExtFile): """Read uncompressed data directly from archive.""" def _open(self): RarExtFile._open(self) self.volfile = self.inf.volume_file self.fd = XFile(self.volfile, 0) self.fd.seek(self.inf.header_offset, 0) self.cur = self.rf._parse_header(self.fd) self.cur_avail = self.cur.add_size def _skip(self, cnt): """RAR Seek, skipping through rar files to get to correct position """ while cnt > 0: # next vol needed? if self.cur_avail == 0: if not self._open_next(): break # fd is in read pos, do the read if cnt > self.cur_avail: cnt -= self.cur_avail self.remain -= self.cur_avail self.cur_avail = 0 else: self.fd.seek(cnt, 1) self.cur_avail -= cnt self.remain -= cnt cnt = 0 def _read(self, cnt): """Read from potentially multi-volume archive.""" buf = [] while cnt > 0: # next vol needed? if self.cur_avail == 0: if not self._open_next(): break # fd is in read pos, do the read if cnt > self.cur_avail: data = self.fd.read(self.cur_avail) else: data = self.fd.read(cnt) if not data: break # got some data cnt -= len(data) self.cur_avail -= len(data) buf.append(data) if len(buf) == 1: return buf[0] return EMPTY.join(buf) def _open_next(self): """Proceed to next volume.""" # is the file split over archives? if (self.cur.flags & RAR_FILE_SPLIT_AFTER) == 0: return False if self.fd: self.fd.close() self.fd = None # open next part self.volfile = self.rf._next_volname(self.volfile) fd = open(self.volfile, "rb", 0) self.fd = fd # loop until first file header while 1: cur = self.rf._parse_header(fd) if not cur: raise BadRarFile("Unexpected EOF") if cur.type in (RAR_BLOCK_MARK, RAR_BLOCK_MAIN): if cur.add_size: fd.seek(cur.add_size, 1) continue if cur.orig_filename != self.inf.orig_filename: raise BadRarFile("Did not found file entry") self.cur = cur self.cur_avail = cur.add_size return True if have_memoryview: def readinto(self, buf): """Zero-copy read directly into buffer.""" got = 0 vbuf = memoryview(buf) while got < len(buf): # next vol needed? if self.cur_avail == 0: if not self._open_next(): break # lenght for next read cnt = len(buf) - got if cnt > self.cur_avail: cnt = self.cur_avail # read into temp view res = self.fd.readinto(vbuf[got : got + cnt]) if not res: break if self.crc_check: self.CRC = crc32(vbuf[got : got + res], self.CRC) self.cur_avail -= res self.remain -= res got += res return got class HeaderDecrypt: """File-like object that decrypts from another file""" def __init__(self, f, key, iv): self.f = f self.ciph = AES.new(key, AES.MODE_CBC, iv) self.buf = EMPTY def tell(self): return self.f.tell() def read(self, cnt=None): if cnt > 8*1024: raise BadRarFile('Bad count to header decrypt - wrong password?') # consume old data if cnt <= len(self.buf): res = self.buf[:cnt] self.buf = self.buf[cnt:] return res res = self.buf self.buf = EMPTY cnt -= len(res) # decrypt new data BLK = self.ciph.block_size while cnt > 0: enc = self.f.read(BLK) if len(enc) < BLK: break dec = self.ciph.decrypt(enc) if cnt >= len(dec): res += dec cnt -= len(dec) else: res += dec[:cnt] self.buf = dec[cnt:] cnt = 0 return res # handle (filename|filelike) object class XFile(object): __slots__ = ('_fd', '_need_close') def __init__(self, xfile, bufsize = 1024): if is_filelike(xfile): self._need_close = False self._fd = xfile self._fd.seek(0) else: self._need_close = True self._fd = open(xfile, 'rb', bufsize) def read(self, n=None): return self._fd.read(n) def tell(self): return self._fd.tell() def seek(self, ofs, whence=0): return self._fd.seek(ofs, whence) def readinto(self, dst): return self._fd.readinto(dst) def close(self): if self._need_close: self._fd.close() def __enter__(self): return self def __exit__(self, typ, val, tb): self.close() ## ## Utility functions ## def is_filelike(obj): if isinstance(obj, str) or isinstance(obj, unicode): return False res = True for a in ('read', 'tell', 'seek'): res = res and hasattr(obj, a) if not res: raise ValueError("Invalid object passed as file") return True def rar3_s2k(psw, salt): """String-to-key hash for RAR3.""" seed = psw.encode('utf-16le') + salt iv = EMPTY h = sha1() for i in range(16): for j in range(0x4000): cnt = S_LONG.pack(i*0x4000 + j) h.update(seed + cnt[:3]) if j == 0: iv += h.digest()[19:20] key_be = h.digest()[:16] key_le = pack("LLLL", key_be)) return key_le, iv def rar_decompress(vers, meth, data, declen=0, flags=0, crc=0, psw=None, salt=None): """Decompress blob of compressed data. Used for data with non-standard header - eg. comments. """ # already uncompressed? if meth == RAR_M0 and (flags & RAR_FILE_PASSWORD) == 0: return data # take only necessary flags flags = flags & (RAR_FILE_PASSWORD | RAR_FILE_SALT | RAR_FILE_DICTMASK) flags |= RAR_LONG_BLOCK # file header fname = bytes('data', 'ascii') date = 0 mode = 0x20 fhdr = S_FILE_HDR.pack(len(data), declen, RAR_OS_MSDOS, crc, date, vers, meth, len(fname), mode) fhdr += fname if flags & RAR_FILE_SALT: if not salt: return EMPTY fhdr += salt # full header hlen = S_BLK_HDR.size + len(fhdr) hdr = S_BLK_HDR.pack(0, RAR_BLOCK_FILE, flags, hlen) + fhdr hcrc = crc32(hdr[2:]) & 0xFFFF hdr = S_BLK_HDR.pack(hcrc, RAR_BLOCK_FILE, flags, hlen) + fhdr # archive main header mh = S_BLK_HDR.pack(0x90CF, RAR_BLOCK_MAIN, 0, 13) + ZERO * (2+4) # decompress via temp rar tmpfd, tmpname = mkstemp(suffix='.rar') tmpf = os.fdopen(tmpfd, "wb") try: tmpf.write(RAR_ID + mh + hdr + data) tmpf.close() cmd = [UNRAR_TOOL] + list(OPEN_ARGS) add_password_arg(cmd, psw, (flags & RAR_FILE_PASSWORD)) cmd.append(tmpname) p = custom_popen(cmd) return p.communicate()[0] finally: tmpf.close() os.unlink(tmpname) def to_datetime(t): """Convert 6-part time tuple into datetime object.""" if t is None: return None # extract values year, mon, day, h, m, xs = t s = int(xs) us = int(1000000 * (xs - s)) # assume the values are valid try: return datetime(year, mon, day, h, m, s, us) except ValueError: pass # sanitize invalid values MDAY = (0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) if mon < 1: mon = 1 if mon > 12: mon = 12 if day < 1: day = 1 if day > MDAY[mon]: day = MDAY[mon] if h > 23: h = 23 if m > 59: m = 59 if s > 59: s = 59 if mon == 2 and day == 29: try: return datetime(year, mon, day, h, m, s, us) except ValueError: day = 28 return datetime(year, mon, day, h, m, s, us) def parse_dos_time(stamp): """Parse standard 32-bit DOS timestamp.""" sec = stamp & 0x1F; stamp = stamp >> 5 min = stamp & 0x3F; stamp = stamp >> 6 hr = stamp & 0x1F; stamp = stamp >> 5 day = stamp & 0x1F; stamp = stamp >> 5 mon = stamp & 0x0F; stamp = stamp >> 4 yr = (stamp & 0x7F) + 1980 return (yr, mon, day, hr, min, sec * 2) def custom_popen(cmd): """Disconnect cmd from parent fds, read only from stdout.""" # needed for py2exe creationflags = 0 if sys.platform == 'win32': creationflags = 0x08000000 # CREATE_NO_WINDOW # run command try: p = Popen(cmd, bufsize = 0, stdout = PIPE, stdin = PIPE, stderr = STDOUT, creationflags = creationflags) except OSError: ex = sys.exc_info()[1] if ex.errno == errno.ENOENT: raise RarCannotExec("Unrar not installed? (rarfile.UNRAR_TOOL=%r)" % UNRAR_TOOL) raise return p def custom_check(cmd, ignore_retcode=False): """Run command, collect output, raise error if needed.""" p = custom_popen(cmd) out, err = p.communicate() if p.returncode and not ignore_retcode: raise RarExecError("Check-run failed") return out def add_password_arg(cmd, psw, required=False): """Append password switch to commandline.""" if UNRAR_TOOL == ALT_TOOL: return if psw is not None: cmd.append('-p' + psw) else: cmd.append('-p-') def check_returncode(p, out): """Raise exception according to unrar exit code""" code = p.returncode if code == 0: return # map return code to exception class errmap = [None, RarWarning, RarFatalError, RarCRCError, RarLockedArchiveError, RarWriteError, RarOpenError, RarUserError, RarMemoryError, RarCreateError, RarNoFilesError] # codes from rar.txt if UNRAR_TOOL == ALT_TOOL: errmap = [None] if code > 0 and code < len(errmap): exc = errmap[code] elif code == 255: exc = RarUserBreak elif code < 0: exc = RarSignalExit else: exc = RarUnknownError # format message if out: msg = "%s [%d]: %s" % (exc.__doc__, p.returncode, out) else: msg = "%s [%d]" % (exc.__doc__, p.returncode) raise exc(msg) # # Check if unrar works # try: # does UNRAR_TOOL work? custom_check([UNRAR_TOOL], True) except RarCannotExec: try: # does ALT_TOOL work? custom_check([ALT_TOOL] + list(ALT_CHECK_ARGS), True) # replace config UNRAR_TOOL = ALT_TOOL OPEN_ARGS = ALT_OPEN_ARGS EXTRACT_ARGS = ALT_EXTRACT_ARGS TEST_ARGS = ALT_TEST_ARGS except RarCannotExec: # no usable tool, only uncompressed archives work pass rarfile-rarfile_2_7/setup.py000066400000000000000000000017011243431105200162770ustar00rootroot00000000000000#! /usr/bin/env python from distutils.core import setup import rarfile ver = rarfile.__version__ ldesc = open("README.rst").read().strip() sdesc = ldesc.split('\n')[0].split(' - ')[1].strip() setup( name = "rarfile", version = ver, description = sdesc, long_description = ldesc, author = "Marko Kreen", license = "ISC", author_email = "markokr@gmail.com", url = "https://github.com/markokr/rarfile", py_modules = ['rarfile'], keywords = ['rar', 'unrar', 'archive'], classifiers = [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: ISC License (ISCL)", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: System :: Archiving :: Compression", ] ) rarfile-rarfile_2_7/test/000077500000000000000000000000001243431105200155455ustar00rootroot00000000000000rarfile-rarfile_2_7/test/Makefile000066400000000000000000000001571243431105200172100ustar00rootroot00000000000000test: ./test.sh clean: rm -rf __pycache__ rm -f files/*.rar.[pj]* *.pyc *.class *.diffs rm -f rarfile.py rarfile-rarfile_2_7/test/files/000077500000000000000000000000001243431105200166475ustar00rootroot00000000000000rarfile-rarfile_2_7/test/files/ctime0.rar000066400000000000000000000001111243431105200205270ustar00rootroot00000000000000Rar!ϐs It .>0 afile.txt:={@rarfile-rarfile_2_7/test/files/ctime0.rar.exp000066400000000000000000000004001243431105200213230ustar00rootroot00000000000000Archive: files/ctime0.rar FILE: hdrlen=46 datlen=0 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=2:WIN ver=29 mode=0x20 meth=0 cmp=0 dec=0 vol=0 crc=0x00000000 (0) time=2011-05-10 21:28:47.899345 name=afile.txt mtime=2011-05-10 21:28:47.899345 rarfile-rarfile_2_7/test/files/ctime1.rar000066400000000000000000000001151243431105200205340ustar00rootroot00000000000000Rar!ϐs t 2>0 afile.txt:>={@rarfile-rarfile_2_7/test/files/ctime1.rar.exp000066400000000000000000000004341243431105200213330ustar00rootroot00000000000000Archive: files/ctime1.rar FILE: hdrlen=50 datlen=0 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=2:WIN ver=29 mode=0x20 meth=0 cmp=0 dec=0 vol=0 crc=0x00000000 (0) time=2011-05-10 21:28:47.899345 name=afile.txt mtime=2011-05-10 21:28:47.899345 ctime=2011-05-10 21:28:47 rarfile-rarfile_2_7/test/files/ctime2.rar000066400000000000000000000001161243431105200205360ustar00rootroot00000000000000Rar!ϐs ҅t 3>0 afile.txt:>={@rarfile-rarfile_2_7/test/files/ctime2.rar.exp000066400000000000000000000004431243431105200213340ustar00rootroot00000000000000Archive: files/ctime2.rar FILE: hdrlen=51 datlen=0 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=2:WIN ver=29 mode=0x20 meth=0 cmp=0 dec=0 vol=0 crc=0x00000000 (0) time=2011-05-10 21:28:47.899345 name=afile.txt mtime=2011-05-10 21:28:47.899345 ctime=2011-05-10 21:28:47.897843 rarfile-rarfile_2_7/test/files/ctime3.rar000066400000000000000000000001171243431105200205400ustar00rootroot00000000000000Rar!ϐs vt 4>0 afile.txt:>:={@rarfile-rarfile_2_7/test/files/ctime3.rar.exp000066400000000000000000000004431243431105200213350ustar00rootroot00000000000000Archive: files/ctime3.rar FILE: hdrlen=52 datlen=0 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=2:WIN ver=29 mode=0x20 meth=0 cmp=0 dec=0 vol=0 crc=0x00000000 (0) time=2011-05-10 21:28:47.899345 name=afile.txt mtime=2011-05-10 21:28:47.899345 ctime=2011-05-10 21:28:47.899327 rarfile-rarfile_2_7/test/files/ctime4.rar000066400000000000000000000001201243431105200205330ustar00rootroot00000000000000Rar!ϐs t 5>0 afile.txt:>:={@rarfile-rarfile_2_7/test/files/ctime4.rar.exp000066400000000000000000000004431243431105200213360ustar00rootroot00000000000000Archive: files/ctime4.rar FILE: hdrlen=53 datlen=0 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=2:WIN ver=29 mode=0x20 meth=0 cmp=0 dec=0 vol=0 crc=0x00000000 (0) time=2011-05-10 21:28:47.899345 name=afile.txt mtime=2011-05-10 21:28:47.899345 ctime=2011-05-10 21:28:47.899345 rarfile-rarfile_2_7/test/files/rar15-comment-lock.rar000066400000000000000000000003221243431105200226720ustar00rootroot00000000000000Rar!@s,bu4J]4@tH0c=3 FILE1.TXTu0file1comment -----ztHC<3c=0 FILE2.TXT'u0pfile2comment -----baaaar rarfile-rarfile_2_7/test/files/rar15-comment-lock.rar.exp000066400000000000000000000010131243431105200234630ustar00rootroot00000000000000Archive: files/rar15-comment-lock.rar comment='RARcomment -----' FILE: hdrlen=72 datlen=7 hdr_unknown=31 flags=0x8008:COMMENT,LONG,D64 os=0:DOS ver=15 mode=0x20 meth=3 cmp=7 dec=7 vol=0 crc=0xe27f07a9 (3799975849) time=2010-11-03 19:49:32 name=FILE1.TXT comment='file1comment -----' FILE: hdrlen=72 datlen=8 hdr_unknown=31 flags=0x8008:COMMENT,LONG,D64 os=0:DOS ver=15 mode=0x20 meth=0 cmp=8 dec=8 vol=0 crc=0x3c4306f7 (1011025655) time=2010-11-03 19:49:38 name=FILE2.TXT comment='file2comment -----' rarfile-rarfile_2_7/test/files/rar15-comment.rar000066400000000000000000000003221243431105200217440ustar00rootroot00000000000000Rar!:"s,bu4J]4@tH0c=3 FILE1.TXTu0file1comment -----ztHC<3c=0 FILE2.TXT'u0pfile2comment -----baaaar rarfile-rarfile_2_7/test/files/rar15-comment.rar.exp000066400000000000000000000010061243431105200225370ustar00rootroot00000000000000Archive: files/rar15-comment.rar comment='RARcomment -----' FILE: hdrlen=72 datlen=7 hdr_unknown=31 flags=0x8008:COMMENT,LONG,D64 os=0:DOS ver=15 mode=0x20 meth=3 cmp=7 dec=7 vol=0 crc=0xe27f07a9 (3799975849) time=2010-11-03 19:49:32 name=FILE1.TXT comment='file1comment -----' FILE: hdrlen=72 datlen=8 hdr_unknown=31 flags=0x8008:COMMENT,LONG,D64 os=0:DOS ver=15 mode=0x20 meth=0 cmp=8 dec=8 vol=0 crc=0x3c4306f7 (1011025655) time=2010-11-03 19:49:38 name=FILE2.TXT comment='file2comment -----' rarfile-rarfile_2_7/test/files/rar202-comment-nopsw.rar000066400000000000000000000003141243431105200231670ustar00rootroot00000000000000Rar!s3u& 4PL m+mtB}znc=0 FILE1.TXT!Qu 0Yfile1commentfile1 X/tB_xqc=0 FILE2.TXTdu 0=^file2commentfile2 rarfile-rarfile_2_7/test/files/rar202-comment-nopsw.rar.exp000066400000000000000000000007731243431105200237730ustar00rootroot00000000000000Archive: files/rar202-comment-nopsw.rar comment='RARcomment' FILE: hdrlen=66 datlen=7 hdr_unknown=25 flags=0x8008:COMMENT,LONG,D64 os=0:DOS ver=20 mode=0x20 meth=0 cmp=7 dec=7 vol=0 crc=0x7a197dba (2048490938) time=2010-11-03 00:27:28 name=FILE1.TXT comment='file1comment' FILE: hdrlen=66 datlen=7 hdr_unknown=25 flags=0x8008:COMMENT,LONG,D64 os=0:DOS ver=20 mode=0x20 meth=0 cmp=7 dec=7 vol=0 crc=0x785fc3e3 (2019541987) time=2010-11-03 00:27:34 name=FILE2.TXT comment='file2comment' rarfile-rarfile_2_7/test/files/rar202-comment-psw.rar000066400000000000000000000003761243431105200226420ustar00rootroot00000000000000Rar!s3u& 4PL m+t B }znc=3 FILE1.TXT!Qu 0Yfile1commentS}~s+*L-5g5/Ƽ%t B _xqc=3 FILE2.TXTdu 0=^file2commentӐCԿyZ|-Iַw^rarfile-rarfile_2_7/test/files/rar202-comment-psw.rar.exp000066400000000000000000000010171243431105200234260ustar00rootroot00000000000000Archive: files/rar202-comment-psw.rar comment='RARcomment' FILE: hdrlen=66 datlen=32 hdr_unknown=25 flags=0x800c:PASSWORD,COMMENT,LONG,D64 os=0:DOS ver=20 mode=0x20 meth=3 cmp=32 dec=7 vol=0 crc=0x7a197dba (2048490938) time=2010-11-03 00:27:28 name=FILE1.TXT comment='file1comment' FILE: hdrlen=66 datlen=32 hdr_unknown=25 flags=0x800c:PASSWORD,COMMENT,LONG,D64 os=0:DOS ver=20 mode=0x20 meth=3 cmp=32 dec=7 vol=0 crc=0x785fc3e3 (2019541987) time=2010-11-03 00:27:34 name=FILE2.TXT comment='file2comment' rarfile-rarfile_2_7/test/files/rar3-comment-hpsw.rar000066400000000000000000000007441243431105200226500ustar00rootroot00000000000000Rar!Ιs :zT$pzs!))@5r=@.a 1[>$咐&dA.B."P]L\r?w ZJ: :zY$HD&VmȗYOA j 4q[idsYnwR5{20t^(:zt5?Na&4Έ6 [U6Zɨx$WtBb);ޚ{xa.Y ɂY! 'Sɘ:z!IʡZZYwQwbAML1ךa YN9(8$Q=wR5{20t^(:z+>Z!4-&X)0mY="s4&O MfgŲt}D.)p7F:z.΅gjd|rarfile-rarfile_2_7/test/files/rar3-comment-hpsw.rar.exp000066400000000000000000000011101243431105200234270ustar00rootroot00000000000000Archive: files/rar3-comment-hpsw.rar comment='RARcomment\n' FILE: hdrlen=51 datlen=16 hdr_unknown=0 flags=0x9424:PASSWORD,SALT,EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=3 cmp=16 dec=0 vol=0 crc=0x00000000 (0) time=2010-11-02 10:03:25 name=file1.txt mtime=2010-11-02 10:03:25 comment='Comment1v2\n' FILE: hdrlen=51 datlen=16 hdr_unknown=0 flags=0x9424:PASSWORD,SALT,EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=3 cmp=16 dec=0 vol=0 crc=0x00000000 (0) time=2010-11-02 10:03:25 name=file2.txt mtime=2010-11-02 10:03:25 comment='Comment2v2\n' rarfile-rarfile_2_7/test/files/rar3-comment-plain.rar000066400000000000000000000004541243431105200227700ustar00rootroot00000000000000Rar!ϐs ҙz# Pu3CMT VH)kjvz t +lPb=3 file1.txtgAz# H?o3CMT 6V,x&oԋ½st +lPb=3 file2.txtg|z# 3CMTsAH'끏W*={@rarfile-rarfile_2_7/test/files/rar3-comment-plain.rar.exp000066400000000000000000000010511243431105200235550ustar00rootroot00000000000000Archive: files/rar3-comment-plain.rar comment='RARcomment\n' FILE: hdrlen=43 datlen=8 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=3 cmp=8 dec=0 vol=0 crc=0x00000000 (0) time=2010-11-02 10:03:25 name=file1.txt mtime=2010-11-02 10:03:25 comment='Comment1v2\n' FILE: hdrlen=43 datlen=8 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=3 cmp=8 dec=0 vol=0 crc=0x00000000 (0) time=2010-11-02 10:03:25 name=file2.txt mtime=2010-11-02 10:03:25 comment='Comment2v2\n' rarfile-rarfile_2_7/test/files/rar3-comment-psw.rar000066400000000000000000000005141243431105200224730ustar00rootroot00000000000000Rar!ϐs ҙz# Pu3CMT VH)kjvzmt$3lPb=3 file1.txt/e'1xlmMjmAz# H?o3CMT 6V,x&oԋ½t$3lPb=3 file2.txt/e'1xlmMjm|z# 3CMTsAH'끏W*={@rarfile-rarfile_2_7/test/files/rar3-comment-psw.rar.exp000066400000000000000000000011071243431105200232650ustar00rootroot00000000000000Archive: files/rar3-comment-psw.rar comment='RARcomment\n' FILE: hdrlen=51 datlen=16 hdr_unknown=0 flags=0x9424:PASSWORD,SALT,EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=3 cmp=16 dec=0 vol=0 crc=0x00000000 (0) time=2010-11-02 10:03:25 name=file1.txt mtime=2010-11-02 10:03:25 comment='Comment1v2\n' FILE: hdrlen=51 datlen=16 hdr_unknown=0 flags=0x9424:PASSWORD,SALT,EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=3 cmp=16 dec=0 vol=0 crc=0x00000000 (0) time=2010-11-02 10:03:25 name=file2.txt mtime=2010-11-02 10:03:25 comment='Comment2v2\n' rarfile-rarfile_2_7/test/files/seektest.rar000066400000000000000000000043151243431105200212070ustar00rootroot00000000000000Rar!ϐs t ,ZŰf>5 stest1.txt ޯ_5uefVH,WF@0 stest2.txt000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 ={@rarfile-rarfile_2_7/test/files/seektest.rar.exp000066400000000000000000000007661243431105200220100ustar00rootroot00000000000000Archive: files/seektest.rar FILE: hdrlen=44 datlen=90 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=3:UNIX ver=29 mode=0100644 meth=5 cmp=90 dec=2048 vol=0 crc=0xc5b7e6a2 (3317163682) time=2011-06-12 12:53:33 name=stest1.txt mtime=2011-06-12 12:53:33 FILE: hdrlen=44 datlen=2048 hdr_unknown=0 flags=0x9020:EXTTIME,LONG,D128 os=3:UNIX ver=20 mode=0100644 meth=0 cmp=2048 dec=2048 vol=0 crc=0xc5b7e6a2 (3317163682) time=2011-06-12 12:53:33 name=stest2.txt mtime=2011-06-12 12:53:33 rarfile-rarfile_2_7/test/files/unicode.rar000066400000000000000000000002431243431105200210020ustar00rootroot00000000000000Rar!;s t6SQg>5уииоотивл.txt0#gt4 SQg>5𝐀𝐁𝐁𝐂.txtwg={@rarfile-rarfile_2_7/test/files/unicode.rar.exp000066400000000000000000000007011243431105200215740ustar00rootroot00000000000000Archive: files/unicode.rar FILE: hdrlen=54 datlen=17 hdr_unknown=0 flags=0x8080:LONG,D1024 os=3:UNIX ver=29 mode=0100644 meth=5 cmp=17 dec=2 vol=0 crc=0x6751fc53 (1733426259) time=2011-07-06 16:48:04 name=уииоотивл.txt FILE: hdrlen=52 datlen=13 hdr_unknown=0 flags=0x8090:SOLID,LONG,D1024 os=3:UNIX ver=29 mode=0100644 meth=5 cmp=13 dec=2 vol=0 crc=0x6751fc53 (1733426259) time=2011-07-06 16:48:04 name=𝐀𝐁𝐁𝐂.txt rarfile-rarfile_2_7/test/test1.sh000077500000000000000000000012701243431105200171440ustar00rootroot00000000000000#! /bin/sh PYTHONPATH=..:$PYTHONPATH export PYTHONPATH JAVA_OPTIONS="-Dpython.path=`pwd`/.." export JAVA_OPTIONS plist="python2.4 python2.5 python2.6 python2.7 python3.1 python3.2 python3.3 python3.4 pypy jython jython2.7" rm -f test.diffs for py in $plist; do if which $py > /dev/null; then for f in files/*.rar; do printf "%s -> %-30s .. " $py $f $py ../dumprar.py -t -t -v -ppassword $f > $f.$py if diff -uw $f.exp $f.$py > /dev/null; then echo "ok" else echo "FAIL" echo "#### $py ####" >> test.diffs diff -uw $f.exp $f.$py >> test.diffs fi done echo "" else echo $py not available echo "" fi done rarfile-rarfile_2_7/test/test2.sh000077500000000000000000000005211243431105200171430ustar00rootroot00000000000000#! /bin/sh cp ../rarfile.py . #ulimit -n 16 plist="python2.4 python2.5 python2.6 python2.7 python3.1 python3.2 python3.3 python3.4 pypy jython jython2.7" for py in $plist; do if which $py > /dev/null; then echo "== $py ==" $py ./testseek.py $py ./testio.py $py ./testcorrupt.py --quick fi done rm -f rarfile.py rarfile-rarfile_2_7/test/testcorrupt.py000077500000000000000000000035171243431105200205260ustar00rootroot00000000000000#! /usr/bin/env python import rarfile import sys, os, time import tempfile def progress(): sys.stdout.write('.') sys.stdout.flush() def try_read(tmpfn): #progress() try: rf = rarfile.RarFile(tmpfn) if rf.needs_password(): rf.setpassword('password') except rarfile.Error: return for fn in rf.namelist(): try: data = rf.read(fn) pass except rarfile.Error: pass def test_rar(rarfn): data = open(rarfn, "rb").read() fd, tmpfn = tempfile.mkstemp('.rar') os.close(fd) print('testcorrupt 1') for n in range(len(data)): bad = data[:n] f = open(tmpfn, 'wb') f.write(bad) f.close() try_read(tmpfn) print('testcorrupt 2') crap = rarfile.RAR_ID for n in range(1, len(data)): for i in range(len(crap)): c = crap[i:i+1] bad = data[:n - 1] + c + data[n:] f = open(tmpfn, 'wb') f.write(bad) f.close() try_read(tmpfn) os.unlink(tmpfn) test_rar_list = [ "files/ctime0.rar", "files/ctime1.rar", "files/ctime2.rar", "files/ctime3.rar", "files/ctime4.rar", "files/seektest.rar", "files/rar15-comment-lock.rar", "files/rar15-comment.rar", "files/rar202-comment-nopsw.rar", "files/rar202-comment-psw.rar", "files/rar3-comment-hpsw.rar", "files/rar3-comment-plain.rar", "files/rar3-comment-psw.rar", "files/unicode.rar", ] def main(): if sys.argv[-1] == '--quick': test_rar("files/rar3-comment-plain.rar") return for rar in test_rar_list: print(rar) test_rar(rar) if __name__ == '__main__': try: main() except OSError: print('OSError: pid = %d' % os.getpid()) time.sleep(80000) rarfile-rarfile_2_7/test/testio.py000077500000000000000000000013631243431105200174340ustar00rootroot00000000000000#! /usr/bin/env python import rarfile, os, os.path, time, sys try: from io import BufferedReader, TextIOWrapper except ImportError: print('no io module') sys.exit(0) def BufferedReader(x): return x def TextIOWrapper(x): return x def test_readline(rf, fn): f = rf.open(fn) tr = TextIOWrapper(BufferedReader(f)) while 1: ln = tr.readline() if not ln: break tr.close() def main(): files = ['stest1.txt', 'stest2.txt'] arc = 'files/seektest.rar' rf = rarfile.RarFile(arc, crc_check=0) for fn in files: sys.stdout.write('test/readline: %s .. ' % fn) sys.stdout.flush() test_readline(rf, fn) print('ok') if __name__ == '__main__': main() rarfile-rarfile_2_7/test/testseek.py000077500000000000000000000042321243431105200177520ustar00rootroot00000000000000#! /usr/bin/env python import rarfile, os, os.path, time, sys def show_fds(): fdir = "/proc/%d/fd" % os.getpid() if os.path.isdir(fdir): os.system('printf "fds = "; ls -l %s | wc -l' % fdir) def do_seek(f, pos, lim): ofs = pos*4 fsize = lim*4 if ofs < 0: exp = 0 elif ofs > fsize: exp = fsize else: exp = ofs f.seek(ofs) got = f.tell() if got != exp: raise Exception('seek failed (got=%d, exp=%d)' % (got, exp)) ln = f.read(4) if got == fsize and ln: raise Exception('unexpected read') if not ln and got < fsize: raise Exception('unexpected read failure') if ln: spos = int(ln) if spos*4 != got: raise Exception('unexpected pos: spos=%d pos=%d' % (spos, pos)) def test_seek(rf, fn): inf = rf.getinfo(fn) cnt = int(inf.file_size / 4) f = rf.open(fn) do_seek(f, int(cnt/2), cnt) do_seek(f, 0, cnt) for i in range(int(cnt/2)): do_seek(f, i*2, cnt) for i in range(cnt): do_seek(f, i*2 - int(cnt / 2), cnt) for i in range(cnt + 10): do_seek(f, cnt - i - 5, cnt) f.close() print('OK') def test_arc(arc, desc): files = ['stest1.txt', 'stest2.txt'] rf = rarfile.RarFile(arc, crc_check=0) for fn in files: sys.stdout.write('%s | test/seek %s .. ' % (desc, fn)) sys.stdout.flush() test_seek(rf, fn) def main(): arc = 'files/seektest.rar' data = open(arc, 'rb').read() # filename test_arc(arc, "fn") # filelike: cStringIO try: import cStringIO test_arc(cStringIO.StringIO(data), "cStringIO") except ImportError: pass # filelike: io.BytesIO, io.open() try: import io test_arc(io.BytesIO(data), "io.BytesIO") test_arc(io.open(arc, 'rb'), "io.open") except ImportError: pass # filelike: StringIO try: import StringIO test_arc(StringIO.StringIO(data), "StringIO") except ImportError: pass # filelike: file() test_arc(open(arc, 'rb'), "file") time.sleep(1) show_fds() if __name__ == '__main__': main()