Search code examples
iosswiftstreamingarchiveunzip

How to unzip a big zip file containing one file and get the progress in bytes with swift?


I try to unzip a big zip file containing only one item (more than 100MB) and like to show the progress during unzipping.

I found solutions where the progress can be determined based on the amount of files unzipped but in my case I have only one big file inside. So I guess it must be determined by the amount of bytes unzipped?

Actually I am using SSZipArchive with the following code which works fine:

    var myZipFile:NSString="/Users/user/Library/Developer/CoreSimulator/Devices/mydevice/ziptest/testzip.zip";
    var DestPath:NSString="/Users/user/Library/Developer/CoreSimulator/Devices/mydevice/ziptest/";


    let unZipped = SSZipArchive.unzipFileAtPath(myZipFile as! String, toDestination: DestPath as! String);

I found no solutions for this.

Does anyone have a hint, sample or link to a sample ?

UPDATE: Following code looks like it would work as intended, but the handler will be called only once (at the end of unzipping) when the only one file is unzipped:

func unzipFile(sZipFile: String, toDest: String){

        SSZipArchive.unzipFileAtPath(sZipFile, toDestination: toDest, progressHandler: {
            (entry, zipInfo, readByte, totalByte) -> Void in


            println("readByte : \(readByte)") // <- This will be only called once, at the end of unzipping. My 500MB Zipfile holds only one file. 
            println("totalByte : \(totalByte)")


            //Asynchrone task
            dispatch_async(dispatch_get_main_queue()) {
                println("readByte : \(readByte)")
                println("totalByte : \(totalByte)")

                //Change progress value

            }
            }, completionHandler: { (path, success, error) -> Void in
                if success {
                    //SUCCESSFUL!!
                } else {
                    println(error)
                }
        })

    }

UPDATE 2:

As "Martin R" analysed in SSArchive, its not possible. Is there any other way to unzip a file and show the progress based kbytes?

UPDATE 3:

I changed the SSZipArchive.m after the solution was explained by "roop" as follows. Probably someone else can use this too:

FILE *fp = fopen((const char*)[fullPath UTF8String], "wb");
                while (fp) {
                    int readBytes = unzReadCurrentFile(zip, buffer, 4096);

                    if (readBytes > 0) {
                        fwrite(buffer, readBytes, 1, fp );
                        totalbytesread=totalbytesread+4096;
                        // Added by me
                        if (progressHandler)
                        {
                            progressHandler(strPath, fileInfo, currentFileNumber, totalbytesread);
                        }
                        // End added by me

                    } else {
                        break;
                    }
                }

Solution

  • To achieve what you want, you will have to modify SSZipArchive's internal code.

    SSZipArchive uses minizip to provide the zipping functionality. You can see the minizip unzipping API here: unzip.h.

    In SSZipArchive.m, you can get the uncompressed size of the file being unzipped from the fileInfo variable.

    You can see that the unzipped contents are being read here:

     FILE *fp = fopen((const char*)[fullPath UTF8String], "wb");
     while (fp) {
         int readBytes = unzReadCurrentFile(zip, buffer, 4096);
         if (readBytes > 0) {
             fwrite(buffer, readBytes, 1, fp );
         } else {
             break;
         }
     }
    

    You will need the readBytes and the uncompressed file size to compute the progress for a single file. You can add a new delegate to SSZipArchive to send these data back to the calling code.