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

📄 package.c

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

#include "package.h"
enum {ADIR = 0x10, VOLUME = 0x08};

BOOL putto_package(TFileInfo *file_info, FILE *input, FILE *output);

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

    DIR *dir = NULL;
    struct dirent *de;
    TFileInfo file_info;
    int pathlen;
    char full_path[PATH_MAX] = {0};
    TInfoHead info_head;
    FILE *output, *input;
    unsigned long int begin, end;

    begin = clock();
    fputs("This program is written by HongGuo Wang.\n", stderr);
    __opendir_flags = __OPENDIR_NO_HIDDEN;
    _djstat_flags = _STAT_INODE | _STAT_EXEC_EXT | _STAT_EXEC_MAGIC | _STAT_DIRSIZE | _STAT_ROOT_TIME | _STAT_WRITEBIT;
    switch(argc) {
        case 2:
            dir = opendir(".\\");
            strcpy(full_path, ".\\");
            pathlen = 2;
            break;
        case 3:
            dir = opendir(*++argv);
            strcpy(full_path, *argv);
            pathlen = strlen(full_path);
            if(full_path[pathlen - 1] != '\\' && full_path[pathlen - 1] != ':') {
                full_path[pathlen] = '\\';
                full_path[++pathlen] = '\0';
            }
            break;
        default:
            fputs("Useage: package [dirname] filename\n", stderr);
            exit(-1);
    }
    if(dir == NULL) {
       fprintf(stderr,"%s: ", full_path);
       perror("Can't open the directory");
       exit(-2);
    }
    if((output = fopen(*++argv, "wb")) == NULL) {
        perror("Can't open the file");
        exit(-3);
    }

    info_head.files = info_head.bytes = 0;
    while((de = readdir(dir)) != NULL) {
         int attr;
         struct stat fs;
         if(stricmp(*argv, de->d_name) == 0 || stricmp("cwsdpmi.swp", de->d_name) == 0)
             continue;
         memset(full_path + pathlen, 0, sizeof(full_path) - pathlen);
         memset(file_info.file_name, 0, FILENAMELEN);
         strcpy(full_path + pathlen, de->d_name);
         strncpy(file_info.file_name, de->d_name, FILENAMELEN);
         attr = _chmod(full_path, 0, 0);
         if(attr == -1 || (attr & ADIR) == ADIR || (attr & VOLUME) == VOLUME)
             continue;
         stat(full_path, &fs);
         file_info.file_length = fs.st_size;
         printf("paking file %12s, length = %8lu...", file_info.file_name, file_info.file_length);
         if((input = fopen(full_path, "rb")) == NULL){
             fprintf(stderr, "Can't put the file %s into package.\n", file_info.file_name);
             fclose(output);
             closedir(dir);
             exit(-2);
         }
         putto_package(&file_info, input, output);
         puts("OK");
         info_head.files++;
         info_head.bytes += file_info.file_length;
    }
    closedir(dir);
    fwrite(&info_head, 1UL, sizeof(info_head), output);
    fclose(output);
    printf("\tTotal %lu file(s), %lu byte(s)\n", info_head.files, info_head.bytes);
    end = clock();
    printf("\tTime used: %lf seconde(s).\n", ((double)end - (double)begin) / CLK_TCK);
    return 0;
}

BOOL putto_package(TFileInfo *file_info, FILE *input, FILE *output) {

    unsigned long int bytes_read;
    char read_buf[BUFLEN];

    if(file_info == NULL || input == NULL || output == NULL)
        return FALSE;

    fwrite(file_info, 1UL, sizeof(TFileInfo), output);
    while(!feof(input) && !ferror(input)) {
        bytes_read = fread(read_buf, 1UL, BUFLEN, input);
        fwrite(read_buf, 1UL, bytes_read, output);
    }
    fclose(input);
    return TRUE;
} /* putto_package */

⌨️ 快捷键说明

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