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
6f193e0e
Kaydet (Commit)
6f193e0e
authored
Ara 27, 2008
tarafından
Antoine Pitrou
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Patch by Gabriel Genellina.
üst
e57e9990
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
14 deletions
+60
-14
zipfile.rst
Doc/library/zipfile.rst
+4
-2
test_zipfile.py
Lib/test/test_zipfile.py
+34
-5
zipfile.py
Lib/zipfile.py
+19
-7
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/zipfile.rst
Dosyayı görüntüle @
6f193e0e
...
...
@@ -66,9 +66,11 @@ The module defines the following items:
.. function:: is_zipfile(filename)
Returns ``True`` if *filename* is a valid ZIP file based on its magic number,
otherwise returns ``False``.
This module does not currently handle ZIP files
which have appended comments.
otherwise returns ``False``.
*filename* may be a file or file-like object too.
This module does not currently handle ZIP files
which have appended comments.
.. versionchanged:: 2.7
Support for file and file-like objects.
.. data:: ZIP_STORED
...
...
Lib/test/test_zipfile.py
Dosyayı görüntüle @
6f193e0e
...
...
@@ -634,20 +634,49 @@ class OtherTests(unittest.TestCase):
def
testIsZipErroneousFile
(
self
):
# This test checks that the is_zipfile function correctly identifies
# a file that is not a zip file
fp
=
open
(
TESTFN
,
"w"
)
fp
.
write
(
"this is not a legal zip file
\n
"
)
fp
.
close
()
# - passing a filename
with
open
(
TESTFN
,
"w"
)
as
fp
:
fp
.
write
(
"this is not a legal zip file
\n
"
)
chk
=
zipfile
.
is_zipfile
(
TESTFN
)
self
.
assert_
(
chk
is
False
)
self
.
assert_
(
not
chk
)
# - passing a file object
with
open
(
TESTFN
,
"rb"
)
as
fp
:
chk
=
zipfile
.
is_zipfile
(
fp
)
self
.
assert_
(
not
chk
)
# - passing a file-like object
fp
=
StringIO
()
fp
.
write
(
"this is not a legal zip file
\n
"
)
chk
=
zipfile
.
is_zipfile
(
fp
)
self
.
assert_
(
not
chk
)
fp
.
seek
(
0
,
0
)
chk
=
zipfile
.
is_zipfile
(
fp
)
self
.
assert_
(
not
chk
)
def
testIsZipValidFile
(
self
):
# This test checks that the is_zipfile function correctly identifies
# a file that is a zip file
# - passing a filename
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"w"
)
zipf
.
writestr
(
"foo.txt"
,
"O, for a Muse of Fire!"
)
zipf
.
close
()
chk
=
zipfile
.
is_zipfile
(
TESTFN
)
self
.
assert_
(
chk
is
True
)
self
.
assert_
(
chk
)
# - passing a file object
with
open
(
TESTFN
,
"rb"
)
as
fp
:
chk
=
zipfile
.
is_zipfile
(
fp
)
self
.
assert_
(
chk
)
fp
.
seek
(
0
,
0
)
zip_contents
=
fp
.
read
()
# - passing a file-like object
fp
=
StringIO
()
fp
.
write
(
zip_contents
)
chk
=
zipfile
.
is_zipfile
(
fp
)
self
.
assert_
(
chk
)
fp
.
seek
(
0
,
0
)
chk
=
zipfile
.
is_zipfile
(
fp
)
self
.
assert_
(
chk
)
def
testNonExistentFileRaisesIOError
(
self
):
# make sure we don't raise an AttributeError when a partially-constructed
...
...
Lib/zipfile.py
Dosyayı görüntüle @
6f193e0e
...
...
@@ -128,18 +128,30 @@ _CD64_NUMBER_ENTRIES_TOTAL = 7
_CD64_DIRECTORY_SIZE
=
8
_CD64_OFFSET_START_CENTDIR
=
9
def
is_zipfile
(
filename
):
"""Quickly see if file is a ZIP file by checking the magic number."""
def
_check_zipfile
(
fp
):
try
:
fpin
=
open
(
filename
,
"rb"
)
endrec
=
_EndRecData
(
fpin
)
fpin
.
close
()
if
endrec
:
return
True
# file has correct magic number
if
_EndRecData
(
fp
):
return
True
# file has correct magic number
except
IOError
:
pass
return
False
def
is_zipfile
(
filename
):
"""Quickly see if a file is a ZIP file by checking the magic number.
The filename argument may be a file or file-like object too.
"""
result
=
False
try
:
if
hasattr
(
filename
,
"read"
):
result
=
_check_zipfile
(
fp
=
filename
)
else
:
with
open
(
filename
,
"rb"
)
as
fp
:
result
=
_check_zipfile
(
fp
)
except
IOError
:
pass
return
result
def
_EndRecData64
(
fpin
,
offset
,
endrec
):
"""
Read the ZIP64 end-of-archive records and use that to update endrec
...
...
Misc/NEWS
Dosyayı görüntüle @
6f193e0e
...
...
@@ -86,6 +86,9 @@ Core and Builtins
Library
-------
- Issue #4756: zipfile.is_zipfile() now supports file-like objects. Patch by
Gabriel Genellina.
- Issue #4400: .pypirc default generated file was broken in distutils.
- Issue #4736: io.BufferedRWPair's closed property now functions properly.
...
...
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