2

I'm working on a python script that will automatically update Calibre since it does not contain an autoupdater and updates weekly. My script downloads the portable zip file, and then should extract a specific subfolder of the zip file (I don't need all the files).

What I have so far:

os.system("7z x install.zip -y -r -oc:\\\"Program Files (x86)\\\"Calibre2");

This works for everything except downloading the specific sub directory I need. Any suggestions?

Jacobm001
  • 4,431
  • 4
  • 30
  • 51

2 Answers2

3

As given in How to partially extract a folder from a 7z file using powershell:

os.system("7z x install.zip subfolder -y -r -oc:\\\"Program Files (x86)\\\"Calibre2")

should be ok for you

Community
  • 1
  • 1
Mat M
  • 1,786
  • 24
  • 30
0

Below is a zip version of a similar answer I gave for tar files.

Usage:

unzipdir.py path/to/zipfile zip/folder [output/folder]

Script:

import sys, os
from zipfile import ZipFile

def get_members(zip, prefix):
    if not prefix.endswith('/'):
        prefix += '/'
    offset = len(prefix)
    for zipinfo in zip.infolist():
        name = zipinfo.filename
        if len(name) > offset:
            zipinfo.filename = name[offset:]
            yield zipinfo

args = sys.argv[1:]

if len(args):
    zip = ZipFile(args[0])
    path = args[2] if len(args) > 2 else '.'
    zip.extractall(path, get_members(zip, args[1]))
Community
  • 1
  • 1
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • @Jacobm001. I tested my script on the [latest calibre version](http://sourceforge.net/projects/calibre/files/), and it works fine (it only took a few seconds to extract the 128Mb `/Calibre Portable/Calibre` folder). So 7zip is not needed. – ekhumoro Jan 15 '12 at 01:51