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

Naa*_*a A 10 gzip unzip google-cloud-storage google-cloud-platform

如何在Goolge Cloud Storage Bucket中解压缩.zip文件?(如果我们有一些其他工具,比如AWS的'CloudBerry Explorer',那就太好了.)

小智 9

以下是我创建的一些代码,用作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();

            });
        }
    })

});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的例子,但是我发现它有一个严重的缺陷:如果你不消耗流,你应该只调用 entry.autodrain() 。否则,您将损坏输出文件。我做到了,直到我更改了代码。 (2认同)

Dan*_*ing 8

您可以使用Python,例如通过Cloud Function:

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
Run Code Online (Sandbox Code Playgroud)


the*_*per 8

如果您最终在您的 Google Cloud Storage 存储桶上有一个 zip 文件,因为您必须使用该gsutil cp命令从另一台服务器移动大文件,您可以在复制时改为 gzip,它将以压缩格式传输并在到达存储桶时解压缩。

它是通过使用 -Z 参数在 gsutil cp 中构建的。

例如

gsutil cp -Z largefile.txt gs://bucket/largefile.txt
Run Code Online (Sandbox Code Playgroud)


小智 6

幸运的是,GCS中没有机制可以解压缩文件。与此相关的功能请求已转发给Google开发团队。

或者,您可以将ZIP文件上传到GCS存储桶,然后将它们下载到附加到VM实例的永久磁盘上,在此处解压缩,然后使用gsutil工具上传解压缩的文件。


小智 6

在shell中,您可以使用以下命令解压缩压缩文件

gsutil cat gs://bucket/obj.csv.gz | zcat |  gsutil cp - gs://bucket/obj.csv
Run Code Online (Sandbox Code Playgroud)


小智 6

谷歌云数据流中有数据流模板,有助于压缩/解压缩云存储中的文件。请参阅下面的屏幕截图

此模板暂存批处理管道,用于将 Cloud Storage 上的文件解压缩到指定位置。当您想要使用压缩数据来最小化网络带宽成本时,此功能非常有用。管道在单次执行期间自动处理多种压缩模式,并根据文件扩展名(.bzip2、.deflate、.gz、.zip)确定要使用的解压缩模式。

管道要求

要解压缩的文件必须采用以下格式之一:Bzip2、Deflate、Gzip、Zip。

在管道执行之前,输出目录必须存在。

  • 仅适用于单个文件夹,不适用于其中有多个目录的情况 (3认同)