【问题标题】:getting the same directory structure after decompressing a zipped file解压压缩文件后得到相同的目录结构
【发布时间】:2016-08-11 15:05:11
【问题描述】:

我从亚马逊服务器下载了 zip 文件(使用 AWS SDK for unity)。该 zip 文件有一个文件夹,里面还有一个包含 png 文件的文件夹。当我从亚马逊服务器获得响应对象时,我将其作为字节数组读取并存储为 .zip 文件。当我双击 zip 文件时,我会在其中获得包含 png 文件的目录和子目录。现在我需要以编程方式解压缩文件。我正在尝试使用 GZipStream 对其进行解压缩,它返回未压缩的字节数组。现在如何保存这个字节数组以便保留我的文件夹结构?另外我不想使用第三方库来解压缩压缩文件.

无效开始() {

    UnityInitializer.AttachToGameObject (this.gameObject);
    client = new AmazonS3Client (mAccKey, mSecretKey, mRegion);
    Debug.Log ("Getting the presigned url\n");
    GetPreSignedUrlRequest request = new GetPreSignedUrlRequest ();
    request.BucketName = mBucketName;
    request.Key = mFileName;
    request.Expires = DateTime.Now.AddMinutes (5);
    request.Protocol = Protocol.HTTP;



    GetObjectRequest requestObject = new GetObjectRequest ();
    requestObject.BucketName = mBucketName;
    requestObject.Key = mFileName;
    Debug.Log ("Requesting for the " + mFileName + " contents" + "from the bucket\n" + mBucketName);

    client.GetObjectAsync(mBucketName, mFileName, (responseObj) =>
        {

            var response = responseObj.Response;
            if (response.ResponseStream != null)
            {

                Debug.Log("Recieving response\n");
                using (BinaryReader bReader=new BinaryReader(response.ResponseStream))
                {
                    byte[] buffer = bReader.ReadBytes((int)response.ResponseStream.Length);


                    var zippedPath=Application.persistentDataPath+"/"+zippedFile;
                    File.WriteAllBytes(zippedPath,buffer);

                    var unZippedPath=Application.persistentDataPath+"/"+unZipToFolder;
                    DirectoryInfo directory=Directory.CreateDirectory(unZippedPath);
                    byte[]compressedData=compress(buffer);

                    byte[] unCompressedData=decompress(compressedData);
                    //Debug.Log(unCompressedData.Length);
                    File.WriteAllBytes(unZippedPath+directory,unCompressedData);








                }
                Debug.Log("Response complete");

            }

        });
}


#region GZipStream
public static byte[] compress(byte[] data)
{
    using (MemoryStream outStream = new MemoryStream())
    {
        using (GZipStream gzipStream = new GZipStream(outStream, CompressionMode.Compress))
        using (MemoryStream srcStream = new MemoryStream(data))
            CopyTo(srcStream, gzipStream);
        return outStream.ToArray();
    }
}


public static byte[] decompress(byte[] compressed)
{
    using (MemoryStream inStream = new MemoryStream(compressed))
    using (GZipStream gzipStream = new GZipStream(inStream, CompressionMode.Decompress))
    using (MemoryStream outStream = new MemoryStream())
    {
        CopyTo(gzipStream, outStream);
        return outStream.ToArray();
    }
}

public static void CopyTo(Stream input, Stream output)
{
    byte[] buffer = new byte[16 * 1024];
    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}

#endregion

}

zip 文件中的文件夹结构 images->示例图像->10 个 png 文件

【问题讨论】:

  • 请添加一些代码,并格式化您的问题,使其更具可读性。先阅读guidelines,会有更多人愿意帮助你;)

标签: c# unity3d


【解决方案1】:

GZipStream 只能(解)压缩流。换句话说,您无法使用它恢复文件夹结构。使用ZipFile,或者,如果您不能使用框架4.5,则使用SharpZipLib

【讨论】:

  • 我想不使用第三方库。
  • 正如我所写,使用 ZipFile,它是 net framework 4.5 的一部分。如果由于某种原因,您不能或不想使用 4.5,则必须使用第三方组件。
  • 感谢您的回复。我没有使用.net framework 4.5。所以使用第三方组件是唯一的解决方案吗?
  • 谢谢尼诺。我使用了 SharpZipLib,并且非常适合我。
  • 谁能告诉我如何使用 aws sdk 获得下载的进度值以实现统一?在使用 s3 从亚马逊服务器下载文件时,我没有获得任何属性/方法提供下载进度。请帮助。
猜你喜欢
  • 2020-05-23
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多