Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
dd25e86c
Kaydet (Commit)
dd25e86c
authored
Şub 07, 2010
tarafından
Ronald Oussoren
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 6003: ZipFile.writestr "compression_type" argument
üst
2bd52dcc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
2 deletions
+30
-2
zipfile.rst
Doc/library/zipfile.rst
+8
-1
test_zipfile.py
Lib/test/test_zipfile.py
+14
-0
zipfile.py
Lib/zipfile.py
+5
-1
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/zipfile.rst
Dosyayı görüntüle @
dd25e86c
...
...
@@ -284,7 +284,7 @@ ZipFile Objects
byte, the name of the file in the archive will be truncated at the null byte.
.. method:: ZipFile.writestr(zinfo_or_arcname, bytes)
.. method:: ZipFile.writestr(zinfo_or_arcname, bytes
[, compress_type]
)
Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file
name it will be given in the archive, or a :class:`ZipInfo` instance. If it's
...
...
@@ -294,6 +294,10 @@ ZipFile Objects
created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling
:meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`.
If given, *compress_type* overrides the value given for the *compression*
parameter to the constructor for the new entry, or in the *zinfo_or_arcname*
(if that is a :class:`ZipInfo` instance).
.. note::
When passing a :class:`ZipInfo` instance as the *zinfo_or_acrname* parameter,
...
...
@@ -301,6 +305,9 @@ ZipFile Objects
member of the given :class:`ZipInfo` instance. By default, the
:class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`.
.. versionchanged:: 2.7
The *compression_type* argument.
The following data attributes are also available:
...
...
Lib/test/test_zipfile.py
Dosyayı görüntüle @
dd25e86c
...
...
@@ -408,6 +408,20 @@ class TestsWithSourceFile(unittest.TestCase):
# remove the test file subdirectories
shutil
.
rmtree
(
os
.
path
.
join
(
os
.
getcwd
(),
'ziptest2dir'
))
def
test_writestr_compression
(
self
):
zipfp
=
zipfile
.
ZipFile
(
TESTFN2
,
"w"
)
zipfp
.
writestr
(
"a.txt"
,
"hello world"
,
compress_type
=
zipfile
.
ZIP_STORED
)
if
zlib
:
zipfp
.
writestr
(
"b.txt"
,
"hello world"
,
compress_type
=
zipfile
.
ZIP_DEFLATED
)
info
=
zipfp
.
getinfo
(
'a.txt'
)
self
.
assertEqual
(
info
.
compress_type
,
zipfile
.
ZIP_STORED
)
if
zlib
:
info
=
zipfp
.
getinfo
(
'b.txt'
)
self
.
assertEqual
(
info
.
compress_type
,
zipfile
.
ZIP_DEFLATED
)
def
zip_test_writestr_permissions
(
self
,
f
,
compression
):
# Make sure that writestr creates files with mode 0600,
# when it is passed a name rather than a ZipInfo instance.
...
...
Lib/zipfile.py
Dosyayı görüntüle @
dd25e86c
...
...
@@ -1063,13 +1063,14 @@ class ZipFile:
self
.
filelist
.
append
(
zinfo
)
self
.
NameToInfo
[
zinfo
.
filename
]
=
zinfo
def
writestr
(
self
,
zinfo_or_arcname
,
bytes
):
def
writestr
(
self
,
zinfo_or_arcname
,
bytes
,
compress_type
=
None
):
"""Write a file into the archive. The contents is the string
'bytes'. 'zinfo_or_arcname' is either a ZipInfo instance or
the name of the file in the archive."""
if
not
isinstance
(
zinfo_or_arcname
,
ZipInfo
):
zinfo
=
ZipInfo
(
filename
=
zinfo_or_arcname
,
date_time
=
time
.
localtime
(
time
.
time
())[:
6
])
zinfo
.
compress_type
=
self
.
compression
zinfo
.
external_attr
=
0600
<<
16
else
:
...
...
@@ -1079,6 +1080,9 @@ class ZipFile:
raise
RuntimeError
(
"Attempt to write to ZIP archive that was already closed"
)
if
compress_type
is
not
None
:
zinfo
.
compress_type
=
compress_type
zinfo
.
file_size
=
len
(
bytes
)
# Uncompressed size
zinfo
.
header_offset
=
self
.
fp
.
tell
()
# Start of header bytes
self
.
_writecheck
(
zinfo
)
...
...
Misc/NEWS
Dosyayı görüntüle @
dd25e86c
...
...
@@ -15,6 +15,9 @@ Core and Builtins
Library
-------
- Issue #6003: add an argument to ``zipfile.Zipfile.writestr`` to
specify the compression type.
What's New in Python 2.7 alpha 3?
=================================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment