如何在谷歌云存储中解压缩.zip文件?

问题描述 投票:6回答:5

如何在Google云端存储分区中解压缩.zip文件? (如果我们有一些其他工具,比如AWS的'CloudBerry Explorer',那就太好了。)

google-cloud-platform google-cloud-storage gzip unzip
5个回答
7
投票

以下是我创建的一些代码,用作Firebase云功能。它旨在通过内容类型“application / zip”监听加载到存储桶中的文件,并将其提取到位。

const functions = require('firebase-functions');
const admin = require("firebase-admin");
const path = require('path');
const fs = require('fs');
const os = require('os');
const unzip = require('unzipper')

admin.initializeApp();

const storage = admin.storage();


const runtimeOpts = {
  timeoutSeconds: 540,
  memory: '2GB'
}

exports.unzip = functions.runWith(runtimeOpts).storage.object().onFinalize((object) => {

    return new Promise((resolve, reject) => {
        //console.log(object)
        if (object.contentType !== 'application/zip') {
          reject();
        } else {
          const bucket = firebase.storage.bucket(object.bucket)
          const remoteFile = bucket.file(object.name)
          const remoteDir = object.name.replace('.zip', '')

          console.log(`Downloading ${remoteFile}`)

          remoteFile.createReadStream()
            .on('error', err => {
              console.error(err)
              reject(err);
            })
            .on('response', response => {
              // Server connected and responded with the specified status and headers.
              //console.log(response)
            })
            .on('end', () => {
              // The file is fully downloaded.
              console.log("Finished downloading.")
              resolve();
            })
            .pipe(unzip.Parse())
            .on('entry', entry => {
              const file = bucket.file(`${remoteDir}/${entry.path}`)

              entry.pipe(file.createWriteStream())
              .on('error', err => {
                console.log(err)
                reject(err);
              })
              .on('finish', () => {
                console.log(`Finsihed extracting ${remoteDir}/${entry.path}`)
              });

              entry.autodrain();

            });
        }
    })

});

5
投票

幸运的是,GCS中没有解压缩文件的机制。有关相同内容的feature request已经转发给Google开发团队。

作为替代方案,您可以将ZIP文件上载到GCS存储桶,然后将其下载到连接到VM实例的永久磁盘,将其解压缩,然后使用gsutil tool上载解压缩的文件。


4
投票

您可以使用Python,例如来自云功能:

from google.cloud import storage
from zipfile import ZipFile
from zipfile import is_zipfile
import io

def zipextract(bucketname, zipfilename_with_path):

    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucketname)

    destination_blob_pathname = zipfilename_with_path

    blob = bucket.blob(destination_blob_pathname)
    zipbytes = io.BytesIO(blob.download_as_string())

    if is_zipfile(zipbytes):
        with ZipFile(zipbytes, 'r') as myzip:
            for contentfilename in myzip.namelist():
                contentfile = myzip.read(contentfilename)
                blob = bucket.blob(zipfilename_with_path + "/" + contentfilename)
                blob.upload_from_string(contentfile)

zipextract("mybucket", "path/file.zip") # if the file is gs://mybucket/path/file.zip

1
投票

我担心默认情况下,Google Cloud中没有可以执行此操作的程序...但是您可以使用此功能,例如使用Python。

您只需输入以下命令:

python

或者如果您需要管理员权限:

sudo python

然后在Python解释器中:

>>> from zipfile import ZipFile
>>> zip_file = ZipFile('path_to_file/t.zip', 'r')
>>> zip_file.extractall('path_to_extract_folder')

最后,按Ctrl + D退出Python解释器。

解压缩的文件将位于您指定的位置(当然,如果您拥有这些位置的相应权限)。

上述方法对Python 2和Python 3的工作方式相同。

尽情享受吧! :)


0
投票

在3.2版或更高版本中使用Python的另一种快速方法:

import shutil
shutil.unpack_archive('filename')

该方法还允许您指示目标文件夹:

shutil.unpack_archive('filename', 'extract_dir')

上述方法不仅适用于zip存档,也适用于tar,gztar,bztar或xztar存档。

如果您需要更多选项,请查看shutil模块的文档:shutil.unpack_archive

© www.soinside.com 2019 - 2024. All rights reserved.