⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unpak.c

📁 一个打包和解包的程序。可以给同一个目录下的文件打包。用 gcc 编译。
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#include "package.h"

int main(int argc, char *argv[]) {

    FILE *input, *output;
    TInfoHead info_head;
    TFileInfo file_info;
    unsigned long int begin, end;
    unsigned long int unpakok = 0UL, total;
    char write_buf[BUFLEN];

    begin = clock();
    fputs("This program is written by HongGuo Wang.\n", stderr);
    if(argc != 2) {
        fputs("Useage: unpak pakfilename\n", stderr);
        exit(-1);
    }
    if((input = fopen(*++argv, "rb")) == NULL) {
        perror("Can't open the package");
        exit(-2);
    }

    fseek(input, -sizeof(TInfoHead), SEEK_END);
    fread(&info_head, 1UL, sizeof(info_head), input);
    printf("\tThere is(are) total %lu file(s) in the package %s\n", info_head.files, *argv);
    rewind(input);
    total = info_head.files;
    /*fread(&file_info, 1UL, sizeof(file_info), input);
    printf("Unpaking file: (%s, %lu)\n", file_info.file_name, file_info.file_length);*/
    while(info_head.files-- >= 1 && !feof(input) && !ferror(input)) {
        long int left;
        unsigned long int bytes_read;
        fread(&file_info, 1UL, sizeof(file_info), input);
        if((output = fopen(file_info.file_name, "wb")) == NULL) {
            fprintf(stdout, "Can't open file %s\n", file_info.file_name);
            fclose(input);
            exit(-3);
        }
        printf("unpaking file %12s, length = %8lu...", file_info.file_name, file_info.file_length);
        left = file_info.file_length;
        while(left >= BUFLEN) {
            left -= bytes_read = fread(write_buf, 1, BUFLEN, input);
            fwrite(write_buf, 1, bytes_read, output);
        }
        if(left > 0) {
            left -= bytes_read = fread(write_buf, 1, left, input);
            fwrite(write_buf, 1, bytes_read, output);
        }
        fclose(output);
        printf("OK (%lu of %lu)\n", ++unpakok, total);
    }
    fclose(input);
    printf("\tTotal %lu file(s) unpaked OK\n", unpakok);
    end = clock();
    printf("\tTime used: %lf second(s)", ((double)end - (double)begin) / CLK_TCK);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -