是否可以在 Google Cloud Storage 中合并 2 个 zip 文件?我可以成功合并非zip文件,但是当我要合并zip文件时,即使文件在云端成功合并,但当我要下载并打开它时,出现文件夹无法正确压缩的错误。我的问题是什么,我做错了什么?你能帮助我吗?谢谢..
示例代码如下:
“SharpZipLib”版本=“1.4.2”
“Google.Cloud.Storage.V1”版本=“4.7.0”
// total 10 files in this directory
string filePath = "C:\\Users\\user\\Desktop\\ziptest";
// chunked 5 files in each list
IEnumerable<string[]> chunkedFilesNames = Directory.GetFiles(filePath).ToList().Chunk(5);
// uploaded files list
List<string> uploadedFiles = new List<string>();
int i = 0;
// chunked files list
foreach (string[] fileNames in chunkedFilesNames)
{
using (MemoryStream outputMemStream = new MemoryStream())
{
using (ZipOutputStream zipStream = new ZipOutputStream(outputMemStream))
{
// each file in chunked files list
foreach (string fileName in fileNames)
{
ZipEntry zipEntry = new ZipEntry(fileName)
{
DateTime = DateTime.Now
};
// put next entry to zip stream
await zipStream.PutNextEntryAsync(zipEntry);
// read file bytes
byte[] bytes = await System.IO.File.ReadAllBytesAsync(fileName);
using (MemoryStream inStream = new MemoryStream(bytes))
{
StreamUtils.Copy(inStream, zipStream, new byte[4096]);
}
// close entry
zipStream.CloseEntry();
}
// chunked zip file name example: test/0_0014b86930954475bb80c79094f690cf.zip
string chunkedZip = $"test/{i}_"+Guid.NewGuid().ToString("N")+".zip";
// add uploaded file to list
uploadedFiles.Add(chunkedZip);
zipStream.IsStreamOwner = false;
zipStream.Close();
// upload zip file to cloud storage
await _cloudStorage.UploadMemoryStream(fileName: chunkedZip, contentType: "multipart/x-zip", stream: outputMemStream);
i++;
}
}
}
// compose uploaded files
await _cloudStorage.ComposeObjects(fileNames: uploadedFiles, uploadPath: "test/composed.zip", contentType: "multipart/x-zip");
文件格式
.gz
和 .tar.gz
实际上接受串联。为了获得想要的结果,您应该考虑将扩展名从 .zip
更改为 .tar.gz
;这将使 Compose 能够按预期运行。