Simple use of SSZipArchive, get the file name without unzipping

SSZipArchive is a very complete compression and decompression tool. Based on the modification and packaging of miniZip, the API is more concise and practical.

SSZipArchive download address: Click to open the link .

There are many articles on the practicality of SSZipArchive. You can quickly understand how it is practical by looking at the header file directly. I will not record it here. Here, I will mainly record the method of obtaining the file name and suffix without decompression, because the file download of the project is in order to improve the download. Speed, the server will compress the file to download in the form of a zip package, and the client will verify the decompression. In this case, if the file suffix is ​​not clear, it cannot be saved in an effective file format. Originally, a stupid method was used to decompress the file into a file. After the folder, the file format is obtained by querying fileManager, then renamed, and then copied to the specified folder, but I feel that the efficiency is low, and it will increase the risk when downloading in multiple threads, so I queried a lot of information and read the unzip.c file The content below, found a way to get the file suffix in the compressed package without decompressing it.


//open a file
    unzFile uf = unzOpen(zipPath.UTF8String);
    //If it is empty, the file does not exist
    if (NULL == uf)
    {
        printf("unzOpen failed...\n");
        return;
    }
    // jump to the first item in the list
    int err = unzGoToFirstFile(uf);
    if (UNZ_OK != err)
    {
        printf("GetFileInZip unzLocateFile failed... error:%d\n",err);
        return;
    }
    
    unz_file_info file_info;
    char filename_inzip[256];
    //Get the file information of the current item
    err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
    if (UNZ_OK != err)
    {
        printf("unzGetCurrentFileInfo failed... error:%d\n", err);
        return ;
    }
    // get the file name
    NSString *fileName = [NSString stringWithFormat:@"%s",filename_inzip];


Here is the processing method of only one file in the zip package. If there are multiple files, the user can traverse the linked list or use goNext to query one by one.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444272&siteId=291194637