我可以使用 msilib 或其他 Python 库从 .msi 文件中提取一个文件吗? [英] Can I use msilib or other Python libraries to extract one file from an .msi file?

查看:19
本文介绍了我可以使用 msilib 或其他 Python 库从 .msi 文件中提取一个文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正想做的是确定 MSI 中的特定文件是否存在并包含特定字符串.

What I really want to do is determine whether a particular file in the MSI exists and contains a particular string.

我目前的想法是运行:

 db = msilib.OpenDatabase('c:\Temp\myfile.msi',1)
 query = "select * from File"
 view = db.OpenView(query)
 view.Execute(None)
 cur_record = view.Fetch()     # do this until I get the record I want
 print cur_record.GetString(3) # do stuff with this value

然后如果它在那里,使用

And then if it's there, extract all the files using

msiexec /a c:\Temp\myfile.msi /qn TARGETDIR=c:\foo

并使用任何解析器来查看我的字符串是否存在.但我希望存在一种不那么笨拙的方式.

and use whatever parser to see whether my string is there. But I'm hoping a less clunky way exists.

推荐答案

请注意,作为 msilib 说,目前没有实现对读取 .cab 文件的支持".和.更一般地说,该库旨在构建 .msi 文件,而不是读取它们.stdlib 中没有任何其他内容可以满足您的需求.

Note that, as the docs for msilib say, "Support for reading .cab files is currently not implemented". And. more generally, the library is designed for building .msi files, not reading them. And there is nothing else in the stdlib that will do what you want.

所以,有几种可能性:

  1. 查找并安装另一个库,例如 pycabinet.我对这个特定的图书馆一无所知;这只是我得到的第一个搜索结果;您可能想自己搜索.但它声称为 CAB 文件提供了一个类似于 zipfile 的 API,这听起来正是您缺少的部分.
  2. 使用 win32com(如果您有 pywin32)或 ctypes(如果您是受虐狂)与底层 COM 对话接口和/或经典的内阁 API(我认为现在已弃用,但仍然有效).
  3. 使用 IronPython 而不是 CPython,这样您就可以使用更简单的 .NET 接口.
  1. Find and install another library, like pycabinet. I know nothing about this particular library; it's just the first search hit I got; you probably want to search on your own. But it claims to provide a zipfile-like API for CAB files, which sounds like exactly the part you're missing.
  2. Use win32com (if you've got pywin32) or ctypes (if you're a masochist) to talk to the underlying COM interfaces and/or the classic Cabinet API (which I think is now deprecated, but still works).
  3. Use IronPython instead of CPython, so you can use the simpler .NET interfaces.

由于我这里没有 Windows 设备,我无法对此进行测试,但这里是 Christopher Painter 用 IronPython 而不是 C# 编写的 .NET 解决方案的草图:

Since I don't have a Windows box here, I can't test this, but here's a sketch of Christopher Painter's .NET solution written in IronPython instead of C#:

import clr
clr.AddReference('Microsoft.Deployment.WindowsInstaller')
clr.AddReference('Microsoft.Deployment.WindowsInstaller.Package')
from Microsoft.Deployment.WindowsInstaller import *
from Microsoft.Deployment.WindowsInstaller.Package import *

def FindAndExtractFiles(packagePath, longFileName):
    with InstallPackage(packagePath, DatabaseOpenMode.ReadOnly) as installPackage:
        if installPackage.FindFiles(longFileName).Count() > 0:
            installPackage.ExtractFiles()

这篇关于我可以使用 msilib 或其他 Python 库从 .msi 文件中提取一个文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆