📄 0712118.cpp
字号:
// 0712118.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main(int argc, char* argv[])
{
FILE *dest_file;
fileheader header;/////////////////
char temp[255],temp2[255];
char Files[100][255];
if (argc==5)
// if(1)
{
if(strcmp(argv[1],"-e")==0)
{
strcpy(temp,argv[2]);
FileNameFromPath(temp);
strcpy(header.file,temp);
header.N=1;
if(IsFolder(argv[2],Files))
{
header.N=IsFolder(argv[2],Files);
strcpy(temp,argv[2]);
FileNameFromPath(temp);
strcat(temp,".huf");
strcpy(temp2,argv[4]);
CreateFileDirectory(temp,temp2);
}
else
{
strcpy(Files[0],argv[2]);
strcpy(temp,argv[2]);
FileNameFromPath(temp);
CreateFileName(temp);
strcpy(temp2,argv[4]);
CreateFileDirectory(temp,temp2);
}
dest_file = fopen(temp2, "wb");
if(!dest_file)
{
printf("Loi mo file.");
getch();
exit(1);
}
Huffman huffman;
for(int i=0;i<header.N;i++)
{
FILE *source_file;
unsigned char *sour, *dest;
int usize, csize;
source_file = fopen(Files[i], "rb");
if(!source_file)
{
printf("Loi mo file %s",Files[i]);
getch();
continue;
}
usize = get_file_size(source_file) ;
header.UnCompressSize[i]=usize;
sour = new unsigned char[usize];
dest = new unsigned char[usize];
usize = (int)fread(sour, 1, usize, source_file);
strcpy(temp,Files[i]);
FileNameFromPath(temp);
strcpy(huffman.TenFile,temp);
huffman.encode(dest, csize, sour, usize);
fwrite(dest,1, csize, dest_file);
header.CompressSize[i]=csize;
}
fwrite(&header,1, sizeof(fileheader), dest_file);
printf("Chuong trinh duoc thuc hien xong.");
}
if(strcmp(argv[1],"-d")==0)
{
FILE *source_file,*dest_file;
Huffman huffman;
unsigned char *sour, *dest;
int usize, csize;
source_file = fopen(argv[2], "rb");
fseek(source_file,-sizeof(fileheader),SEEK_END);
fread(&header,sizeof(fileheader),1,source_file);
rewind(source_file);
if(header.N==1)
{
csize = header.CompressSize[0];
sour = new unsigned char[csize];
csize = fread(sour,1, csize, source_file);
dest = new unsigned char[header.UnCompressSize[0]];
huffman.filesize=header.UnCompressSize[0];
huffman.decode(dest, usize, sour);
strcpy(temp,huffman.TenFile);
strcpy(temp2,argv[4]);
strcat(temp2,"\\");
strcat(temp2,temp);
dest_file=fopen(temp2,"wb");
fwrite(dest,1,usize, dest_file);
}
else
{
for(int i=0;i<header.N;i++)
{
unsigned char *sour, *dest;
int usize, csize;
csize = header.CompressSize[i];
sour = new unsigned char[csize];
csize = fread(sour,1, csize, source_file);
dest = new unsigned char[header.UnCompressSize[i]];
huffman.filesize=header.UnCompressSize[i];
huffman.decode(dest, usize, sour);
strcpy(temp2,argv[4]);
strcpy(temp,argv[4]);
strcat(temp,"\\");
strcat(temp,header.file);
strcat(temp,"\\");
_mkdir(temp);
strcat(temp,huffman.TenFile);
dest_file=fopen(temp,"wb");
fwrite(dest,1,usize, dest_file);
}
}
printf("Chuong trinh duoc thuc hien xong.");
}
}
else
if(argc==3 && strcmp(argv[1],"-l")==0)
{
unsigned char *sour, *dest;
int usize, csize;
Huffman huffman;
FILE *source_file;
source_file = fopen(argv[2], "rb");
if(!source_file)
{
printf("Khong mo duoc tap tin.");
getch();
exit(0);
}
fseek(source_file,-sizeof(fileheader),SEEK_END);
fread(&header,sizeof(fileheader),1,source_file);
rewind(source_file);
if(header.N!=1)
{
printf("\n{%s: ",header.file);
for(int i=0;i<header.N;i++)
{
csize = header.CompressSize[i];
sour = new unsigned char[csize];
csize = fread(sour,1, csize, source_file);
dest = new unsigned char[header.UnCompressSize[i]];
huffman.filesize=header.UnCompressSize[i];
huffman.decode(dest, usize, sour);
strcpy(temp,huffman.TenFile);
printf("%s",temp);
if(i<(header.N-1))
printf(", ");
else
printf("}");
}
}
else
{
csize = header.CompressSize[0];
sour = new unsigned char[csize];
csize = fread(sour,1, csize, source_file);
dest = new unsigned char[header.UnCompressSize[0]];
huffman.filesize=header.UnCompressSize[0];
huffman.decode(dest, usize, sour);
strcpy(temp,huffman.TenFile);
printf("{");
printf("%s",temp);
printf("}");
}
}
else
if(argc==7 && strcmp(argv[3],"-f")==0)
{
FILE *source_file,*dest_file;
Huffman huffman;
unsigned char *sour, *dest;
int usize, csize;
int sl_file;
source_file = fopen(argv[2], "rb");
fseek(source_file,-sizeof(fileheader),SEEK_END);
fread(&header,sizeof(fileheader),1,source_file);
if(header.N==4)
printf("Thanh cong!");
rewind(source_file);
sl_file=GetFileName(argv[4],Files);
for(int i=0;i<header.N;i++)
{
csize = header.CompressSize[i];
sour = new unsigned char[csize];
csize = fread(sour,1, csize, source_file);
dest = new unsigned char[header.UnCompressSize[i]];//huffman.get_uncompressed_size(sour)]; --> 3 la size usize luc encoding//-----
huffman.filesize=header.UnCompressSize[i]; //--> 3 la size usize luc encoding--------
huffman.decode(dest, usize, sour);
strcpy(temp,argv[6]);
strcat(temp,"\\");
strcat(temp,header.file);
strcat(temp,"\\");
_mkdir(temp);
strcat(temp,huffman.TenFile);
strcpy(temp2,huffman.TenFile);
if(IsFileDecoding(temp2,Files,sl_file))
{
dest_file=fopen(temp,"wb");
fwrite(dest,1,usize, dest_file);
}
}
printf("Chuong trinh duoc thuc hien xong.");
}
else
{
printf("Tham so dong lenh sai");
printf("\n0712118.exe -e <File_Name> -o <Output_Directory>");
printf("\n0712118.exe -e <Directory_Name> -o <Output_Directory>");
printf("\n0712118.exe -l <Compressed_File>");
printf("\n0712118.exe -d <Compressed_File> -o <Output_Directory>");
printf("\n0712118.exe -d <Compressed_File> -f file1,file2,...,fileN -o <Output_Directory>");
getch();
exit(0);
}
getch();
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -