简体   繁体   English

使用tarfile和urllib使用python从ftp站点打开tarfile

[英]opening a tarfile from an ftp site using python using tarfile and urllib

I'm trying to download a file from inside a tar file on an ftp server. 我正在尝试从ftp服务器上的tar文件内部下载文件。 similar to this Read contents of .tar.gz file from website into a python 3.x object when i go to open the tarfile i get an ReadError (below) 与此类似,当我去打开tarfile时, 从网站将.tar.gz文件的内容读取到python 3.x对象中 ,我得到一个ReadError(如下)

ftpURL = u'ftp://ftpprd.ncep.noaa.gov/pub/data/nccf/com/wave/prod/multi_1.20170201/multi_1.t00z.spec_tar.gz'
ftpstream = urllib.urlopen(ftpURL)
tar = tarfile.open(fileobj=ftpstream, mode='r|bz2')    # here's where i get the error 
Traceback (most recent call last):
  File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-c3e97355618c>", line 1, in <module>
    tar = tarfile.open(fileobj=ftpstream, mode='r|bz2')
  File "C:\Anaconda2\lib\tarfile.py", line 1703, in open
    t = cls(name, filemode, stream, **kwargs)
  File "C:\Anaconda2\lib\tarfile.py", line 1587, in __init__
    self.firstmember = self.next()
  File "C:\Anaconda2\lib\tarfile.py", line 2355, in next
    tarinfo = self.tarinfo.fromtarfile(self)
  File "C:\Anaconda2\lib\tarfile.py", line 1251, in fromtarfile
    buf = tarfile.fileobj.read(BLOCKSIZE)
  File "C:\Anaconda2\lib\tarfile.py", line 579, in read
    buf = self._read(size)
  File "C:\Anaconda2\lib\tarfile.py", line 598, in _read
    raise ReadError("invalid compressed data")
ReadError: invalid compressed data

Am i missing something with the buffer size? 我是否缺少缓冲区大小的内容? If so, not being familiar with buffer size, where would i find particular information regarding the needed buffer size, I've to double and triple the size to no avail. 如果是这样,不熟悉缓冲区大小,那么在哪里可以找到有关所需缓冲区大小的特定信息,我必须将缓冲区大小增加一倍和两倍,但无济于事。 I've also tried a few files. 我也尝试了一些文件。 I'm able to download the file manually and open it on my machine.... any help is much appreciated 我可以手动下载文件并在计算机上打开它。...非常感谢您的帮助

Look closer at the signature: 仔细看看签名:

tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)

And the description: 以及说明:

If given, fileobj may be any object that has a read() or write() method (depending on the mode). 如果给定,fileobj可以是具有read()或write()方法的任何对象(取决于模式)。 bufsize specifies the blocksize and defaults to 20 * 512 bytes. bufsize指定块大小,默认为20 * 512字节。 Use this variant in combination with eg sys.stdin, a socket file object or a tape device. 将此变体与sys.stdin,套接字文件对象或磁带设备结合使用。 However, such a TarFile object is limited in that it does not allow random access, see Examples. 但是,这样的TarFile对象受到限制,因为它不允许随机访问,请参见示例。

What you meant to do was: 您的意思是:

tar = tarfile.open(fileobj=ftpstream, mode='r|bz2')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM