📄 war.cpp
字号:
//compress define
#include <stdio.h>
#define VERBOSE /* If defined, prints verbose
program progress when it's
running... */
short father[512];
unsigned short code[256], heap_length;
unsigned long compress_charcount, file_size, heap[257];
unsigned char code_length[256];
long frequency_count[512];
short decomp_tree[512];
unsigned short ucode[256];
unsigned long ufile_size;
unsigned char ucode_length[256];
void build_decomp_tree (), decompress_image ();
FILE *ifile, *ofile;
void build_code_tree (), build_initial_heap ();
void compress_image (), compression_report ();
void get_frequency_count ();
unsigned short generate_code_table ();
#include "stdafx.h"
#include "windows.h"
#include<stdlib.h>
#define MAC_FILENAMELENOPATH 50//用来保存完整的文件夹路径
char war[50], unwar[50], com[50], uncom[50];//打包和解包文件路径
int num = 0;//记录得到的文件数目
int Dnum = 0;//记录得到的文件夹数目
//文件夹结构体
struct Directory
{
char path[100];//用来保存完整的文件夹路径
};
struct Directory Dnode[1000];//保存文件夹信息的数组
//文件结构体
struct Filename
{
char path[100];//文件的完整路径
char name[100];//文件的全名
char mothername[100];//文件的上层路径的名字
double fcount;//计数器,方便从.war文件中逐个读取文件
};
struct Filename Fnode[10000];//保存文件信息的数组
//遍历源文件夹的函数原型
void FindFileInDir(char *pname, char* rootDir);
/* pname是当前文件或文件夹的路径
rootDir是当前要遍历的文件夹路径
strRet用来保存文件名
*/
//打包文件夹的函数原型
void makewar(char* war);
/*
war选择生成war的路径
*/
//解包的函数原型
void extract(char*war,char* unwar);
/*war是打包文件所在路径
unwar是解包路径
*/
int main(/*int argc, char *argv[]*/)
{
char strPath[102400];//源文件夹缓冲区
char pathname[50];//当前文件或文件夹缓冲区
void compress();
void uncompress();
ZeroMemory(war,50);//置零
ZeroMemory(pathname, MAC_FILENAMELENOPATH);
printf("输入文件夹路径\n");
scanf("%s",strPath);//从标准输入得到源文件夹路径
printf("输入压缩文件名\n");
scanf("%s",com);
printf("输入解压缩路径\n");
scanf("%s",unwar);//取得解包路径
printf("\nSearching files...\n");
FindFileInDir(pathname, strPath);//遍历源文件夹
printf("\nSearching files ends\n");
printf("\n");
strcpy(war,"d:\\temp.war");
printf("Compress begins...\n");
makewar(war);//打包处理
compress();
printf("\nCompress ends\n");
printf("\n");
strcpy(uncom,"d:\\temp1.war");
uncompress();
extract(uncom,unwar);//解包处理
remove("d:\\temp.war");
remove("d:\\temp1.war");
printf("\nComplete successfully !\n");
printf("\n");
return 0;
}
//打包
void makewar(char* war)
{ int count=0, n;//计数器,赋给文件结构中的fcount
FILE *in,*out;
out = fopen(war,"wb"); //打开写入.war的文件流
for(n=0;n<num;n++) /* 循环,遍历文件信息数组*/
{ /*通过得到文件在源文件的位置将文件*/
if((in=fopen(Fnode[n].path,"rb"))==NULL) /*逐个拷贝到.war文件中*/
printf("can't open the file\n"); /*并计数每个文件在.war中的位置到count*/
while (!feof(in))
{
char ch=fgetc(in);
if(!feof(in))
{
fputc(ch,out);
count += 1;
}
}
Fnode[n].fcount = count;
fclose(in);
}
fclose(out);
}
void compress()
{
if ((ifile = fopen (war, "rb")) != NULL)
{
fseek (ifile, 0L, 2);//#define SEEK_CUR 1 #define SEEK_END 2 #define SEEK_SET 0
file_size = (unsigned long) ftell (ifile);
fseek (ifile, 0L, 0);
get_frequency_count ();
build_initial_heap ();
build_code_tree ();
if (!generate_code_table ())
printf ("ERROR! Code Value Out of Range. Cannot Compress.\n");
else
{
if ((ofile = fopen (com, "wb")) != NULL)
{
fwrite (&file_size, sizeof (file_size), 1, ofile);
fwrite (code, 2, 256, ofile);
fwrite (code_length, 1, 256, ofile);
fseek (ifile, 0L, 0);
compress_image ();
fclose (ofile);
}
#ifdef VERBOSE
compression_report ();
#endif
}
fclose (ifile);
}
}
/**************************************************************************
COMPRESS_IMAGE ()
This function performs the actual data compression.
**************************************************************************/
void compress_image ()
{
register unsigned int thebyte = 0;
register short loop1;
register unsigned short current_code;
register unsigned long loop;
unsigned short current_length, dvalue;
unsigned long curbyte = 0;
short curbit = 7;
for (loop = 0L; loop < file_size; loop++)
{
dvalue = (unsigned short) getc (ifile);
current_code = code[dvalue];
current_length = (unsigned short) code_length[dvalue];
for (loop1 = current_length-1; loop1 >= 0; --loop1)
{
if ((current_code >> loop1) & 1)
thebyte |= (char) (1 << curbit);
if (--curbit < 0)
{
putc (thebyte, ofile);
thebyte = 0;
curbyte++;
curbit = 7;
}
}
}
putc (thebyte, ofile);
compress_charcount = ++curbyte;
}
/**************************************************************************
COMPRESSION_REPORT ()
This function displays the results of the compression sequence.
**************************************************************************/
void compression_report ()
{
float savings;
unsigned short header_charcount;
unsigned long output_characters;
header_charcount = 768 + sizeof (file_size);
output_characters = (unsigned long) header_charcount +
compress_charcount;
printf ("\nRaw characters : %ld\n", file_size);
printf ("Header characters : %d\n", header_charcount);
printf ("Compressed characters : %ld\n", compress_charcount);
printf ("Total output characters : %ld\n", output_characters);
savings = 100 - ((float) output_characters / (float) file_size) * 100;
printf ("Percentage savings : %3.2f%%\n", savings);
}
/**************************************************************************
GENERATE_CODE_TABLE ()
This function generates the compression code table.
**************************************************************************/
unsigned short generate_code_table ()
{
register unsigned short loop;
register unsigned short current_length;
register unsigned short current_bit;
unsigned short bitcode;
short parent;
for (loop = 0; loop < 256; loop++)
if (frequency_count[loop])
{
current_length = bitcode = 0;
current_bit = 1;
parent = father[loop];
while (parent)
{
if (parent < 0)
{
bitcode += current_bit;
parent = -parent;
}
parent = father[parent];
current_bit <<= 1;
current_length++;
}
code[loop] = bitcode;
if (current_length > 16)
return (0);
else
code_length[loop] = (unsigned char) current_length;
}
else
code[loop] = code_length[loop] = 0;
return (1);
}
/**************************************************************************
BUILD_CODE_TREE ()
This function builds the compression code tree.
**************************************************************************/
void build_code_tree ()
{
void reheap (unsigned short heap_entry);
register unsigned short findex;
register unsigned long heap_value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -