Download zip file from server.same size but can't unzip

I download a zip file from server.Here is my code
`public async Task DownloadFile(string ImageToDownload,ProgressBar progressReporter,CancellationToken ct)
{
long receivedBytes = 0;
long totalBytes = 0;

        HttpClient client = new HttpClient();

        using (var stream = await client.GetStreamAsync(ImageToDownload))
        {
            byte[] buffer = new byte[4096];
            totalBytes = stream.Length;
            System.Diagnostics.Debug.WriteLine("totalBytes:{0}", totalBytes);
            for (;;)
            {
                int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    await Task.Yield();
                    System.Diagnostics.Debug.WriteLine("bytesRead break");
                    break;
                }
                receivedBytes += bytesRead;
                int received = unchecked((int)receivedBytes);
                int total = unchecked((int)totalBytes);
                System.Diagnostics.Debug.WriteLine("received:{0}", received);
                double percentage = ((float)received) / total;
                System.Diagnostics.Debug.WriteLine("percentage:{0}", percentage);
                Device.BeginInvokeOnMainThread(() =>
                {
                    progressReporter.Progress = percentage;
                });
            }
            await DownloadCompleted(stream, ct);

        }
        return receivedBytes;
    }
    private async Task DownloadCompleted(Stream inputStream, CancellationToken ct)
    {

            IFolder rootFolder = FileSystem.Current.LocalStorage;
            IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
            CreationCollisionOption.OpenIfExists);
            IFile file = await folder.CreateFileAsync("cn_1.0.0.zip", CreationCollisionOption.ReplaceExisting, ct);
        using (Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite, ct))
        {
            byte[] buffer = new byte[inputStream.Length];
            int bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length);
            stream.Write(buffer,0,buffer.Length);
        }
    }`

after download finish.I check the file size in ios Simulator Is correct (filepath like this /Users/liangshuang/Library/Developer/CoreSimulator/Devices/5E5037EB-9758-409D-83A7-B836891E1B79/data/Containers/Data/Application/85350C84-1A87-4556-87E2-0F88A43018D1/Library/MySubFolder/cn_1.0.0.zip)。But when I try to unzip the file use “Archive Utility” I got file like this "cn_1.0.0.zip.cpgz".Any one have idea?THX

Tagged:

Best Answer

Answers

  • tematimtematim FRMember ✭✭

    Hello,

    This is my way to unzip file :
    /// <summary> /// Unzip a file on the internal storage /// </summary> /// <param name="path"></param> /// <returns></returns> public async Task<bool> UnzipFileAsync(string path) { string localFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string filePath = Path.GetDirectoryName(path); try { ZipEntry entry = new ZipEntry(Path.GetFileNameWithoutExtension(path)); var fileStreamIn = new FileStream(localFolder + path, FileMode.Open, FileAccess.Read); var zipInStream = new ZipInputStream(fileStreamIn); entry = zipInStream.GetNextEntry(); while (entry != null && entry.CanDecompress) { var fileStreamOut = new FileStream(localFolder + Path.GetDirectoryName(path) + @"/" + entry.Name, FileMode.Create, FileAccess.Write); int size; byte[] buffer = new byte[4096]; do { size = zipInStream.Read(buffer, 0, buffer.Length); fileStreamOut.Write(buffer, 0, size); } while (size > 0); fileStreamOut.Close(); entry = zipInStream.GetNextEntry(); } zipInStream.Close(); fileStreamIn.Close(); } catch(Exception e) { return false; } return true; }

    It's working, you will to adapt it because I work with paths only.

    Regards

Sign In or Register to comment.