C语言打包解包程序
这个程序接着我的上一篇打包解包程序,对上一篇的程序进行了改进,把冗长的打包程序分解成了几个小程序,把匹配打包与全部打包融合在了一起,算是对上一篇程序的改进吧。
头文件pack.h
#ifndef PACH_H
#define PACH_H
#define MAX_PATH 128
#define MAX_NAME 32
typedef struct file { //存储文件和文件夹信息 打包解包时使用
char name[MAX_NAME];
_fsize_t size;
int bol; //区分文件与文件夹 1文件夹 0文件
} file;
typedef struct folder_info { //存储绝对偏移量(相对于文件开头的偏移量)查看打包文件信息时使用
char str[MAX_NAME];
int begin;
int end;
long int offset;
struct folder_info *next;
} folder;
extern void pack();
extern void pack_t(char *path, char *pkfile, char *opt);
extern void pack_l(char *pkfile);
extern void pack_g(char *upkfile, char *fdpath);
#endif
函数实现文件pack.c
库函数和头文件包含
#include<stdio.h>
#include<string.h>
#include<io.h>
#include<stdlib.h>
#include"path.h"
pack用法pack()
void pack()
{
printf("\npack的用法:\n\n");
printf("\t-t\t打包一个文件\n");
printf("\t-g\t解压一个打包文件\n");
printf("\t-l\t查看打包文件信息\n");
printf("\t-r\t打包匹配文件\n\n");
return;
}
打包程序pack_t()
int bol = 0; //值来自argv[4] 用来判断是否删除原文件夹
int stat_info(char *fdpath, file *farray, char *mstr);
int pack_folder(char *fpath);
int copy(char *pkfile, int fsum, file *farray, char *fdpath) //fdpath是绝对路径 fsum是数组farray的大小
{ //pkfile是最终打包到的文件的绝对路径
int size = 0;
FILE *in = fopen(pkfile, "wb");
fwrite(&fsum, sizeof(int), 1, in);
size += sizeof(int);
if(fsum == 0) {
fclose(in);
return size;
}
fwrite(farray, sizeof(file), fsum, in);
size += fsum * sizeof(file);
for(int i = 0; i < fsum; ++i) {
char path[MAX_PATH];
strcpy(path, fdpath);
strcat(path, farray[i].name);
if(farray[i].bol) {