Article View: pl.comp.lang.python
Article #157Re: gzip
From: Adam Karpierz
Date: Wed, 04 Dec 2002 07:06
Date: Wed, 04 Dec 2002 07:06
51 lines
2455 bytes
2455 bytes
Uzytkownik "Pawel J Maczewski" <pjmacz@slcj.uw.edu.pl> napisal:
> Czesc,
> Kolejne moje glupiutkie pytanie:
Nie ma glupkowatych pytan.
Sa jedynie glupkowate odpowiedzi (j/n).
> chcialbym strukture danych ktora
> zapisuje do pliku compresowac (oszczednosc miejsca w moim przypadku ok
> 80%). Wiec mam pytanie: czy istnieje jakis modul to umozliwiajacy
> (to=kompresje i dekompresje) bezposrednio z poziomu pythona? czy tez mam
> import os i przez wywolanie komend systemowych dzialac? ;)
A poszukal w standardowej bibliotece chociaz ?:)
Widnieje tam sobie gzip.py.
Ale zgoda ze nie ma wprost 'odgzipowania' archiwum
wiec zalaczam co trzeba (sprawdzone pobieznie):
import os
import gzip
def ungzip(archive, dstDir=None):
outfile = os.path.basename(archive)
if outfile.endswith('.gz'):
outfile = os.path.splitext(outfile)[0]
if dstDir is not None:
outfile = os.path.join(dstDir, outfile)
istream = gzip.open(archive, 'rb')
try:
if os.path.isfile(outfile):
os.remove(outfile)
ostream = open(outfile, 'wb')
try:
while 1:
buff = istream.read(524288)
if not buff:
break
ostream.write(buff)
except:
ostream.close()
if os.path.isfile(outfile):
os.remove(outfile)
raise
ostream.close()
finally:
istream.close()
Adam Karpierz
karpierz@alpha.pl
Technical Details
Message-ID:
<ask9uk$p19$1@news.tpi.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!news.tele.dk!news.tele.dk!small.news.tele.dk!opentransit.net!newsfeed.tpinternet.pl!news.tpi.pl!not-for-mailReferences:
<ask7ug$8ko$3@news.tpi.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!news.tele.dk!news.tele.dk!small.news.tele.dk!opentransit.net!newsfeed.tpinternet.pl!news.tpi.pl!not-for-mail From: "Adam Karpierz" <karpierz@_NOSPAM_alpha.pl> Newsgroups: pl.comp.lang.python Subject: Re: gzip Date: Wed, 4 Dec 2002 08:06:32 +0100 Organization: tp.internet - http://www.tpi.pl/ Lines: 54 Message-ID: <ask9uk$p19$1@news.tpi.pl> References: <ask7ug$8ko$3@news.tpi.pl> NNTP-Posting-Host: ppp167.intertele.pl X-Trace: news.tpi.pl 1038986005 25641 194.204.186.167 (4 Dec 2002 07:13:25 GMT) X-Complaints-To: usenet@tpi.pl NNTP-Posting-Date: Wed, 4 Dec 2002 07:13:25 +0000 (UTC) X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MSMail-Priority: Normal X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit