zoukankan      html  css  js  c++  java
  • zlib和minizip实现解压zip

    zlib和minizip实现解压zip

    原文连接: https://www.cnblogs.com/mtcnn/p/9410039.html

    #include <stdio.h>
    #include <string.h>
    
    #include "unzip.h"
    
    #define dir_delimter '/'
    #define MAX_FILENAME 512
    #define READ_SIZE 8192
    
    int main( int argc, char **argv )
    {
        if ( argc < 2 )
        {
            printf( "usage:
    %s {file to unzip}
    ", argv[ 0 ] );
            return -1;
        }
    
        // Open the zip file
        unzFile *zipfile = unzOpen( argv[ 1 ] );
        if ( zipfile == NULL )
        {
            printf( "%s: not found
    " );
            return -1;
        }
    
        // Get info about the zip file
        unz_global_info global_info;
        if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
        {
            printf( "could not read file global info
    " );
            unzClose( zipfile );
            return -1;
        }
    
        // Buffer to hold data read from the zip file.
        char read_buffer[ READ_SIZE ];
    
        // Loop to extract all files
        uLong i;
        for ( i = 0; i < global_info.number_entry; ++i )
        {
            // Get info about current file.
            unz_file_info file_info;
            char filename[ MAX_FILENAME ];
            if ( unzGetCurrentFileInfo(
                zipfile,
                &file_info,
                filename,
                MAX_FILENAME,
                NULL, 0, NULL, 0 ) != UNZ_OK )
            {
                printf( "could not read file info
    " );
                unzClose( zipfile );
                return -1;
            }
    
            // Check if this entry is a directory or file.
            const size_t filename_length = strlen( filename );
            if ( filename[ filename_length-1 ] == dir_delimter )
            {
                // Entry is a directory, so create it.
                printf( "dir:%s
    ", filename );
                mkdir( filename );
            }
            else
            {
                // Entry is a file, so extract it.
                printf( "file:%s
    ", filename );
                if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
                {
                    printf( "could not open file
    " );
                    unzClose( zipfile );
                    return -1;
                }
    
                // Open a file to write out the data.
                FILE *out = fopen( filename, "wb" );
                if ( out == NULL )
                {
                    printf( "could not open destination file
    " );
                    unzCloseCurrentFile( zipfile );
                    unzClose( zipfile );
                    return -1;
                }
    
                int error = UNZ_OK;
                do    
                {
                    error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
                    if ( error < 0 )
                    {
                        printf( "error %d
    ", error );
                        unzCloseCurrentFile( zipfile );
                        unzClose( zipfile );
                        return -1;
                    }
    
                    // Write data to file.
                    if ( error > 0 )
                    {
                        fwrite( read_buffer, error, 1, out ); // You should check return of fwrite...
                    }
                } while ( error > 0 );
    
                fclose( out );
            }
    
            unzCloseCurrentFile( zipfile );
    
            // Go the the next entry listed in the zip file.
            if ( ( i+1 ) < global_info.number_entry )
            {
                if ( unzGoToNextFile( zipfile ) != UNZ_OK )
                {
                    printf( "cound not read next file
    " );
                    unzClose( zipfile );
                    return -1;
                }
            }
        }
    
        unzClose( zipfile );
    
        return 0;
    }
    contrib/minizip/$ gcc -I../.. -o unzip uzip.c unzip.c ioapi.c ../../libz.a
    contrib/minizip/$ ./unzip.exe /j/zlib-125.zip
    
  • 相关阅读:
    Linux内核分析第七周学习笔记——Linux内核如何装载和启动一个可执行程序
    <深入理解计算机系统>第七章读书笔记
    Linux内核分析第六周学习笔记——分析Linux内核创建一个新进程的过程
    linux内核分析第3章&第18章读书笔记
    Linux内核分析第五周学习总结——分析system_call中断处理过程
    课本第五章读书笔记
    Linux内核分析第四周学习总结——系统调用的工作机制
    课本第一二章读书笔记
    20162328蔡文琛 实验二 树
    20162328蔡文琛 大二week07
  • 原文地址:https://www.cnblogs.com/cqyp/p/13754315.html
Copyright © 2011-2022 走看看