#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<io.h>
#include<iostream>
#include<string>
using namespace std;
int count=0;
typedef struct Note{
char path[50];
struct Note *next;
}Note,*Lnote;
char path[50];
struct Note *next;
}Note,*Lnote;
typedef struct Node {
char name[20];
unsigned int size;
char name[20];
unsigned int size;
struct Node *next;
}Node,*List;
List creat(void);
void add(List *head,char *Tname,int Tsize);
void bwrite(char *fn);
void file_in(char *Path_r,char *Path_w);
void unzip(char *Path_file,char *Path_out);
void lookthepack(char *path);
Lnote Tcreat(void);
void Tadd(Lnote *Thead,char *Path);
void zip(char *ar1,char *ar2);
void lookthepack(char *path){
int i,j;
Node p;
FILE *fp;
if((fp=fopen(path,"rb"))==NULL){
printf("Can't open %s\n",path);
exit(1);
}
fseek(fp,0L,SEEK_SET);
rewind(fp);
fread(&j,sizeof(j),1,fp);
printf("打包文件总数为: %d\n",j);
for(i=0;i<j;i++){
fread(&p,sizeof(p),1,fp);
printf("%s\n",p.name);
}
}
void zip(char *ar1,char *ar2){
char check;
int l;
List head=creat();
Lnote Thead=Tcreat();
long Handle;
struct _finddata_t FileInfo;
l=strlen(ar1);
for(;l>0;l--){
if(ar1[l-1]=='\\'){
check=ar1[l];
break;}
}
if(check!='*')
strcat(ar1,"\\*.*");
if((Handle=_findfirst(ar1,&The FileInfo)) == -. 1) {
/ * Here is the case where the path does not include wildcard * /
printf ( "path does not exist \ n-!");
Exit (0);
}
the else {
do {
(!. "" (strcmp (FileInfo.name, "..") && strcmp (FileInfo.name,))) {IF
Continue;
}
char path_a [40];
/ * Get the following is intended to remove the file path * / after wildcard
int str_leng, T;
strcpy (path_a, AR1);
str_leng = strlen (path_a);
for (T + = str_leng. 1; ; T--)
{IF (path_a [-T. 1] == '\\') {
path_a [T] = '\ 0';
BREAK;}
the else
path_a [T] = '0';
}
char Tname [20 is ];
strcpy (Tname, FileInfo.name);
strcat (path_a, Tname); // get the absolute path
}Node,*List;
List creat(void);
void add(List *head,char *Tname,int Tsize);
void bwrite(char *fn);
void file_in(char *Path_r,char *Path_w);
void unzip(char *Path_file,char *Path_out);
void lookthepack(char *path);
Lnote Tcreat(void);
void Tadd(Lnote *Thead,char *Path);
void zip(char *ar1,char *ar2);
void lookthepack(char *path){
int i,j;
Node p;
FILE *fp;
if((fp=fopen(path,"rb"))==NULL){
printf("Can't open %s\n",path);
exit(1);
}
fseek(fp,0L,SEEK_SET);
rewind(fp);
fread(&j,sizeof(j),1,fp);
printf("打包文件总数为: %d\n",j);
for(i=0;i<j;i++){
fread(&p,sizeof(p),1,fp);
printf("%s\n",p.name);
}
}
void zip(char *ar1,char *ar2){
char check;
int l;
List head=creat();
Lnote Thead=Tcreat();
long Handle;
struct _finddata_t FileInfo;
l=strlen(ar1);
for(;l>0;l--){
if(ar1[l-1]=='\\'){
check=ar1[l];
break;}
}
if(check!='*')
strcat(ar1,"\\*.*");
if((Handle=_findfirst(ar1,&The FileInfo)) == -. 1) {
/ * Here is the case where the path does not include wildcard * /
printf ( "path does not exist \ n-!");
Exit (0);
}
the else {
do {
(!. "" (strcmp (FileInfo.name, "..") && strcmp (FileInfo.name,))) {IF
Continue;
}
char path_a [40];
/ * Get the following is intended to remove the file path * / after wildcard
int str_leng, T;
strcpy (path_a, AR1);
str_leng = strlen (path_a);
for (T + = str_leng. 1; ; T--)
{IF (path_a [-T. 1] == '\\') {
path_a [T] = '\ 0';
BREAK;}
the else
path_a [T] = '0';
}
char Tname [20 is ];
strcpy (Tname, FileInfo.name);
strcat (path_a, Tname); // get the absolute path
Tadd(&Thead,path_a);
add(&head,FileInfo.name,FileInfo.size);
count++;
}while(_findnext(Handle,&FileInfo)==0);
_findclose(Handle);
The FILE * fp_stu, * fp_txt;
IF ((fp_stu = the fopen (AR2 of, "ab & +")) == NULL) {
the printf ( "Can Not Open ++++% S \ n-", AR2 of);
Exit (. 1);
}
the else {
rewind (fp_stu);
fwrite (& COUNT, the sizeof (COUNT),. 1, fp_stu); // record the total number of documents to an int, and on the package header
Node * on;
op=head->next;
while(op!=NULL){
fwrite(op,sizeof(*op),1,fp_stu);
op=op->next;
}
fclose(fp_stu);
}
Note *pt;
pt=Thead->next;
while(pt!=NULL){
file_in(pt->path,ar2); //文件读取及写入
pt=pt->next;
}
}
}
while(op!=NULL){
fwrite(op,sizeof(*op),1,fp_stu);
op=op->next;
}
fclose(fp_stu);
}
Note *pt;
pt=Thead->next;
while(pt!=NULL){
file_in(pt->path,ar2); //文件读取及写入
pt=pt->next;
}
}
}
void unzip(char *Path_file,char *Path_out){
FILE *fp1,*fp2,*fp3;
char ch;
int num,m=0;
Node p;
if((fp1=fopen(Path_file,"rb"))==NULL){
printf("Can't open %s\n",Path_file);
exit(1);
}
else if((fp2=fopen(Path_file,"rb"))==NULL){
printf("Can't open %s\n",Path_file);
exit(1);
}
else{
fseek(fp1,0L,SEEK_SET);
fseek(fp2,0L,SEEK_SET);
rewind(fp1);
rewind(fp1);
fread(&num,sizeof(num),1,fp1);
printf("The file-count is : %d\n",num);
fseek(fp2,sizeof(int),SEEK_SET);
fseek(fp2,sizeof(p)*num,SEEK_CUR);
do { m++;
fread(&p,sizeof(p),1,fp1);
char fullpath_out[40];
char name[20];
int i=0;
strcpy(name,p.name);
strcpy(fullpath_out,Path_out);
strcat(fullpath_out,name);
if((fp3=fopen(fullpath_out,"ab+"))==NULL){
printf("Can't open %s \n",fullpath_out);
exit(1);
}
else{
for(i=0;i<p.size;i+=(sizeof(char))){
fread(&ch,sizeof(ch),1,fp2);
fwrite(&ch,sizeof(ch),1,fp3);
}
}
}while(m<num);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
}
void file_in(char *Path_r,char *Path_w){
FILE *fpr,*fpw;
char ch;
if((fpr=fopen(Path_r,"rb"))==NULL){
printf("Can't open 文件路径 %s\n",Path_r);
exit(1);
}
if((fpw=fopen(Path_w,"ab+"))==NULL){
printf("Can't open 打包指向路径 %s\n",Path_w);
exit(1);
}
fseek(fpw,0L,SEEK_END);
while ((ch=fgetc(fpr))!=EOF)
fputc(ch,fpw);
fclose(fpr);
fclose(fpw);
}
Lnote Tcreat(void){
Lnote Thead=(Lnote)malloc(sizeof(Note));
Thead->next=NULL;
return Thead;
}
void Tadd(Lnote *Thead,char *Path){
Note *s,*r;
r=*Thead;
while(r->next!=NULL){
r=r->next;
}
s=(Lnote)malloc(sizeof(Note));
strcpy(s->path,Path);
r->next=s;
s->next=NULL;
}
List creat(void){
List head=(List)malloc(sizeof(Node));
head->next=NULL;
return head;
}
void add(List *head,char *Tname,int Tsize){
int i=0;
Node *s,*r;
r=*head;
while(r->next!=NULL){
r=r->next;
}
s=(List)malloc(sizeof(Node));
strcpy(s->name,Tname);
s->size=Tsize;
r->next=s;
s->next=NULL;
}
main int (int argc, char * the argv []) {
IF (argc> =. 1) {
IF (strcmp (the argv [. 1], "Help") == 0) {
the printf ( "This command line parameters are as follows: \ n-");
the printf (" command a total of% D \ n-", argc +. 1);
for (int I = 0; I <argc; I ++)
the printf ("% S% D \ n-", the argv [I], I);
}
}
IF (argc> =. 1) {
IF (strcmp (the argv [. 1], "Help") == 0) {
the printf ( "This command line parameters are as follows: \ n-");
the printf (" command a total of% D \ n-", argc +. 1);
for (int I = 0; I <argc; I ++)
the printf ("% S% D \ n-", the argv [I], I);
}
}
When / * in correspondence with argv [1] is '-l', denotes a display compressed files in the packet * /
char A [20 is], B [. 5];
for (int I = 0; I <2; I ++) {
B [I] = the argv [. 1] [I];
}
B [2] = '\ 0';
IF (strcmp (the argv [. 1], "- L") == 0) {
lookthepack (the argv [2]) ;
}
/ * corresponding to the following argv [1] is '-u', represents decompression * /
the else IF (strcmp (B, "- U") == 0) {
the printf ( "sasda \ n-");
the unzip ( the argv [2], the argv [. 3]);
}
the else {
ZIP (the argv [. 1], the argv [2]);
}
return 0;
/ * command following examples * /
// E: \ packing case \ test.exe E : \ packing case \ Thepack \ * * E: \ packing case \
// E: \ packing case \ test.exe -l E: \ packing case \ testqq.txt
// E: \ packing case \ test.exe - u E: \ packing case \ testqq.txt E: \ packing case \ zxcv \
char A [20 is], B [. 5];
for (int I = 0; I <2; I ++) {
B [I] = the argv [. 1] [I];
}
B [2] = '\ 0';
IF (strcmp (the argv [. 1], "- L") == 0) {
lookthepack (the argv [2]) ;
}
/ * corresponding to the following argv [1] is '-u', represents decompression * /
the else IF (strcmp (B, "- U") == 0) {
the printf ( "sasda \ n-");
the unzip ( the argv [2], the argv [. 3]);
}
the else {
ZIP (the argv [. 1], the argv [2]);
}
return 0;
/ * command following examples * /
// E: \ packing case \ test.exe E : \ packing case \ Thepack \ * * E: \ packing case \
// E: \ packing case \ test.exe -l E: \ packing case \ testqq.txt
// E: \ packing case \ test.exe - u E: \ packing case \ testqq.txt E: \ packing case \ zxcv \
}
I have not done and folders recursively, meaning that only the file folder apply. The program also almost empty supplemented on after compression ,,.