用于制作Windows XP可读ZIP文件的脚本 [英] Script to make Windows XP-readable ZIP file

查看:77
本文介绍了用于制作Windows XP可读ZIP文件的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正准备分发一个Windows XP Python程序和一些

辅助文件,

我希望将所有内容放在.ZIP存档中。它被证明是非常困难的,我想我会在这里发布我的解决方案。是否有一个更好的



假设你在目录c:\\\\b中有一组文件和一些

额外

c:\\\\\\\\\\\\\\\\\\\\\\\使用Python脚本,你会想要制作一个Windows可读的存档(.zip)来保存这个

目录结构,并且存档的根目录是

c:\\\\ b。换句话说,来自c:\\\\ b的所有文件都出现在没有路径前缀的

存档

中,以及c:\中的所有文件a \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ br />
函数os.walk。创建一个zip文件,调用os.walk,并将这些文件添加到

这样的存档如下:


import os

导入zipfile


z =

zipfile.ZipFile(r" c:\\\\\\\\\\\\\ ;,mode =" w",compr ession = zipfile.ZIP_DEFLATED)


for dirpath,dirs,os.walk中的文件(r" c:\\\\ b" ):

文件中的a_file:

a_path = os.path.join(dirpath,a_file)

z.write(a_path)#更改,请参阅下面

z.close()


这会创建一个可以由WinZip或其他Python读取的存档

脚本
使用zipfile的
。但是,当您尝试使用Windows

压缩文件夹

查看器查看它时,它将显示为空。如果你试图解压缩文件

(因为

你知道它们确实存在),你会得到一个Windows安全警告和

XP

拒绝解压缩文件夹 - XP显然害怕它可能是

禽流感

或者别的什么。


如果您将标记为#Change的行更改为z.write(a_path,file),

明确命名

每个文件,现在压缩文件夹查看器将显示存档中的所有文件。

XP不会将其视为病毒,它会提取文件。

但是,

存档不包含子目录;所有文件都在一个

目录中。


一些实验表明Windows不喜欢任何文件名



存档以c:开头的驱动器指示符开头,或者有一个

路径包含

a前导斜杠,如\\\ b\afile.txt" ;.相对路径如

" subdir \afile.txt"是
好​​的,并且在提取存档时会导致所需的行为,例如,创建一个新的目录子目录,并在其中放置afile.txt。


由于ZipFile.write方法需要每个文件的有效路径名,

原始问题的正确

解决方案需要弄乱操作系统的

当前

工作目录。将CWD放在所需的基本目录

存档中,

使用相对路径名将文件添加到存档中,并将

放入CWD返回

当你开始的时候:


导入os

导入zipfile


z =

zipfile.ZipFile(r" c:\\\\ myzip.zip",mode =" w",compr ession = zipfile.ZIP_DEFLATED)


cwd = os.getcwd()

os.chdir(base_dir)

尝试:

for dirpath ,dirs,os.walk中的文件(''''):#这将开始步行

CWD

文件中的a_file:

a_path = os.path.join(dirpath,a_file)

z.write(a_path,a_path)#第二个参数可以省略

吗?

z.close()

终于:

os.chdir(cwd)


这会产生一个存档可以通过Windows XP使用其内置的

功能,WinZip或其他功能提取Python脚本。现在我已经有了b

$ b的解决方案似乎有道理,但是当我开始时它并不是很明显。


Paul Cornelius

解决方案

我在win2000

" z.write(a_path, a_path)"可能会更改为z.write(a_path)

但dirpath不在zipfile

谁可以告诉我?


pac写道:

假设您在目录c:\\\\b中有一组文件,而另一些
附加
c:\ a\b\subdir中的文件。使用Python脚本,你会想要制作一个Windows可读的存档(.zip)来保存这个目录结构,以及存档的根目录是什么c:\ a\b。换句话说,来自c:\\\\ b的所有文件都出现在没有路径前缀的
存档中,并且c:\\\\\\\\\\\\\\\\\\\\\\\\\一个路径
前缀\ sububir。

这看起来应该很容易使用模块zipfile和方便的功能os.walk。创建一个zip文件,调用os.walk,并将文件添加到存档中,如下所示:

import os
import zipfile

z =
zipfile.ZipFile(r" c:\\\\ myzip.zip",mode =" w",compr ession = zipfile.ZIP_DEFLATED)

>对于dirpath,dirs,os.walk中的文件(rc:\\\):
对于文件中的a_file:
a_path = os.path.join(dirpath,a_file)
z.write(a_path)#更改,见下文
z.close()


(旁白:发帖时注意不要使用标签。我怀疑f-bot

将告诉你上面的代码不起作用。)

一些实验表明Windows不喜欢任何文件名

存档中,以c:开头的驱动器指示符开头,或者有一个包含
前导斜杠的路径,如\\\\\\\\\\\ 。相对路径,如
" subdir \afile.txt"是好的,并且在提取存档时会导致所需的行为,例如,创建一个新的目录子目录并在其中放置afile.txt。

由于该方法ZipFile.write需要每个文件的有效路径名,对原始问题的正确解决方案需要弄乱操作系统的当前
工作目录。


ZipFile.write为存档提供可选的第二个参数

filename。你可以做这样的事情(未经测试):


for dirpath,dirs,os.walk中的文件(rc:\\\):

for a_file in files:

a_path = os.path.join(dirpath,a_file)

z_path = a_path [7:]#或者其他什么

z.write(a_path,z_path)

z.close()


并且可能使用一个辅助函数而不是字符串切换到

使它更加健壮(它违反DRY,我不高兴地认为os.walk返回的

dirpath与前缀完全相同

参数)。

将CWD放在存档所需的基本目录中,
使用相对路径名将文件添加到存档中,并且把CWD重新打开了
你刚开始时的位置:




这可能是最好的方式,除非你有某些理由不是

Carl Banks


" PAC" < pa*@fernside.com>写道:

我正在准备发布一个Windows XP Python程序和一些
辅助文件,
我想把所有东西都放在.ZIP档案中。它被证明是非常困难的,我想我会在这里发布我的解决方案。有更好的吗?




可能是异端,但是我使用Ant来做这些事情(使用我的Perl项目):
http://ant.apache.org/


-

John MexIT: http://johnbokma.com/mexit /

个人页面: http://johnbokma.com/

经验丰富的程序员: http://castleamber.com/

快乐的客户: http://castleamber.com/testimonials .html


I''m preparing to distribute a Windows XP Python program and some
ancillary files,
and I wanted to put everything in a .ZIP archive. It proved to be
inordinately
difficult and I thought I would post my solution here. Is there a
better one?

Suppose you have a set of files in a directory c:\a\b and some
additional
files in c:\a\b\subdir. Using a Python script, you would
like to make a Windows-readable archive (.zip) that preserves this
directory structure, and where the root directory of the archive is
c:\a\b. In other words, all the files from c:\a\b appear in the
archive
without a path prefix, and all the files in c:\a\b\subdir have a path
prefix of \subdir.

This looks like it should be easy with module zipfile and the handy
function os.walk. Create a zip file, call os.walk, and add the files
to
the archive like so:

import os
import zipfile

z =
zipfile.ZipFile(r"c:\a\b\myzip.zip",mode="w",compr ession=zipfile.ZIP_DEFLATED)

for dirpath,dirs,files in os.walk(r"c:\a\b"):
for a_file in files:
a_path = os.path.join(dirpath,a_file)
z.write(a_path) # Change, see below
z.close()

This creates an archive that can be read by WinZip or by another Python
script
that uses zipfile. But when you try to view it with the Windows
compressed folder
viewer it will appear empty. If you try to extract the files anyway
(because
you know they are really there), you get a Windows Security Warning and
XP
refuses to decompress the folder - XP is apparently afraid it might be
bird flu
or something.

If you change the line marked #Change to "z.write(a_path,file)",
explicitly naming
each file, now the compressed folder viewer will show all the files in
the archive.
XP will not treat it like a virus and it will extract the files.
However, the
archive does not contain a subdirectory; all the files are in a single
directory.

Some experimentation suggests that Windows does not like any filename
in the
archive that begins with either a drive designator like c:, or has a
path containing
a leading slash like "\a\b\afile.txt". Relative paths like
"subdir\afile.txt" are
okay, and cause the desired behavior when the archive is extracted,
e.g., a new directory subdir is created and afile.txt is placed in it.

Since the method ZipFile.write needs a valid pathname for each file,
the correct
solution to the original problem entails messing around with the OS''s
current
working directory. Position the CWD in the desired base directory of
the archive,
add the files to the archive using their relative pathnames, and put
the CWD back
where it was when you started:

import os
import zipfile

z =
zipfile.ZipFile(r"c:\a\b\myzip.zip",mode="w",compr ession=zipfile.ZIP_DEFLATED)

cwd = os.getcwd()
os.chdir(base_dir)
try:
for dirpath,dirs,files in os.walk(''''): # This starts the walk at
the CWD
for a_file in files:
a_path = os.path.join(dirpath,a_file)
z.write(a_path,a_path) # Can the second argument be
omitted?
z.close()
finally:
os.chdir(cwd)

This produces an archive that can be extracted by Windows XP using its
built-in
capability, by WinZip, or by another Python script. Now that I have
the solution it
seems to make sense, but it wasn''t at all obvious when I started.

Paul Cornelius

解决方案

i am in win2000
"z.write(a_path,a_path)" may change to "z.write(a_path)"
but the dirpath is not in zipfile
who can tell me?


pac wrote:

Suppose you have a set of files in a directory c:\a\b and some
additional
files in c:\a\b\subdir. Using a Python script, you would
like to make a Windows-readable archive (.zip) that preserves this
directory structure, and where the root directory of the archive is
c:\a\b. In other words, all the files from c:\a\b appear in the
archive
without a path prefix, and all the files in c:\a\b\subdir have a path
prefix of \subdir.

This looks like it should be easy with module zipfile and the handy
function os.walk. Create a zip file, call os.walk, and add the files
to
the archive like so:

import os
import zipfile

z =
zipfile.ZipFile(r"c:\a\b\myzip.zip",mode="w",compr ession=zipfile.ZIP_DEFLATED)

for dirpath,dirs,files in os.walk(r"c:\a\b"):
for a_file in files:
a_path = os.path.join(dirpath,a_file)
z.write(a_path) # Change, see below
z.close()
(Aside: be careful not to use tabs when posting. I suspect the f-bot
will be here to tell you that the above code doesn''t work.)
Some experimentation suggests that Windows does not like any filename
in the
archive that begins with either a drive designator like c:, or has a
path containing
a leading slash like "\a\b\afile.txt". Relative paths like
"subdir\afile.txt" are
okay, and cause the desired behavior when the archive is extracted,
e.g., a new directory subdir is created and afile.txt is placed in it.

Since the method ZipFile.write needs a valid pathname for each file,
the correct
solution to the original problem entails messing around with the OS''s
current
working directory.
ZipFile.write takes an optional second argument for the archive
filename. You could have done something like this (untested):

for dirpath,dirs,files in os.walk(r"c:\a\b"):
for a_file in files:
a_path = os.path.join(dirpath,a_file)
z_path = a_path[7:] # or whatever
z.write(a_path,z_path)
z.close()

And maybe use a little helper function instead of the string slice to
make it more robust (it violates DRY, and I''m not happy to assume the
dirpath returned by os.walk has exactly the same prefix as the
argument).
Position the CWD in the desired base directory of
the archive,
add the files to the archive using their relative pathnames, and put
the CWD back
where it was when you started:



This may be the best way anyways, unless you have some reason to not
change the current directory.
Carl Banks


"pac" <pa*@fernside.com> wrote:

I''m preparing to distribute a Windows XP Python program and some
ancillary files,
and I wanted to put everything in a .ZIP archive. It proved to be
inordinately
difficult and I thought I would post my solution here. Is there a
better one?



heresy maybe, but I use Ant to do such things (with my Perl projects):
http://ant.apache.org/

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html


这篇关于用于制作Windows XP可读ZIP文件的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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