uint32_t file_lz77_compress (char *filename_in, char *filename_out, size_t malloc_size, uint8_t pointer_length_width) { FILE *in, *out; uint8_t *uncompressed_text, *compressed_text; uint32_t uncompressed_size, compressed_size; in = fopen(filename_in, "r"); if(in == NULL) return 0; uncompressed_size = fsize(in); uncompressed_text = malloc(uncompressed_size); if((uncompressed_size != fread(uncompressed_text, 1, uncompressed_size, in))) return 0; fclose(in); compressed_text = malloc(malloc_size); compressed_size = lz77_compress(uncompressed_text, uncompressed_size, compressed_text, pointer_length_width); out = fopen(filename_out, "w"); if(out == NULL) return 0; if((compressed_size != fwrite(compressed_text, 1, compressed_size, out))) return 0; fclose(out); return compressed_size; }
/** * 压缩一个文本文件,然后将压缩之后的内容保存为另外一个文件 */ void lz77_compress_test(){ FILE *file = NULL; file = fopen("D:/c_note.txt", "rb"); if (file == NULL) { printf("open file err\n"); return; } fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); printf("file 'size : %ld\n", size); const unsigned char *buf = (unsigned char *)malloc(size * sizeof(unsigned char)); fread((void *)buf, size, sizeof(unsigned char), file); unsigned char *compressed; int compressed_size = 0; if((compressed_size = lz77_compress(buf, &compressed, size)) <= 0) { printf("%s\n", "compress fail"); return; } printf("compressed content size : %d\n", compressed_size); FILE *file2 = NULL; file2 = fopen("D:/c_note.lz77", "wb"); if (file2 == NULL) { printf("%s\n", "write file open err"); return; } fwrite((const void *)(compressed), compressed_size, sizeof(unsigned char), file2); printf("%s\n", "write file end"); fclose(file2); fclose(file); return; }
int main(int argc, char **argv) { FILE *fp; unsigned char original[DATSIZ], *compressed, *restored; int csize, osize, rsize, c, i; /***************************************************************************** * * * Read some data to compress. * * * *****************************************************************************/ if ((fp = fopen("sample.txt", "r")) == NULL) return 1; i = 0; while ((c = getc(fp)) != EOF && i < DATSIZ) { original[i] = c; i++; } osize = i; fclose(fp); /***************************************************************************** * * * Compress the data using Huffman coding. * * * *****************************************************************************/ fprintf(stdout, "Compressing with Huffman coding\n"); fprintf(stdout, "Compressing..."); if ((csize = huffman_compress(original, &compressed, osize)) < 0) { fprintf(stdout, "\n"); return 1; } fprintf(stdout, "Done\n"); fprintf(stdout, "Uncompressing..."); if ((rsize = huffman_uncompress(compressed, &restored)) < 0) { fprintf(stdout, "\n"); free(compressed); return 1; } fprintf(stdout, "Done\n"); fprintf(stdout, "osize=%d, csize=%d, rsize=%d\n", osize, csize, rsize); if (rsize != osize) { fprintf(stdout, "Data was not properly restored\n"); free(compressed); free(restored); return 1; } else { for (i = 0; i < rsize; i++) { if (original[i] != restored[i]) { fprintf(stdout, "Data was not properly restored\n"); if (isgraph(original[i])) fprintf(stdout, "original[%d]=\"%c\"\n", i, original[i]); else fprintf(stdout, "original[%d]=0x%02x\n", i, original[i]); if (isgraph(restored[i])) fprintf(stdout, "restored[%d]=\"%c\"\n", i, restored[i]); else fprintf(stdout, "restored[%d]=0x%02x\n", i, restored[i]); free(compressed); free(restored); return 1; } } } fprintf(stdout, "Data was restored OK\n"); free(compressed); free(restored); /***************************************************************************** * * * Compress some data using LZ77. * * * *****************************************************************************/ fprintf(stdout, "Compressing with LZ77\n"); fprintf(stdout, "Compressing..."); if ((csize = lz77_compress(original, &compressed, osize)) < 0) return 1; fprintf(stdout, "Done\n"); fprintf(stdout, "Uncompressing..."); if ((rsize = lz77_uncompress(compressed, &restored)) < 0) { fprintf(stdout, "\n"); free(compressed); return 1; } fprintf(stdout, "Done\n"); fprintf(stdout, "osize=%d, csize=%d, rsize=%d\n", osize, csize, rsize); if (rsize != osize) { fprintf(stdout, "Data was not properly restored\n"); free(compressed); free(restored); return 1; } else { for (i = 0; i < rsize; i++) { if (original[i] != restored[i]) { fprintf(stdout, "Data was not properly restored\n"); if (isgraph(original[i])) fprintf(stdout, "original[%d]=\"%c\"\n", i, original[i]); else fprintf(stdout, "original[%d]=0x%02x\n", i, original[i]); if (isgraph(restored[i])) fprintf(stdout, "restored[%d]=\"%c\"\n", i, restored[i]); else fprintf(stdout, "restored[%d]=0x%02x\n", i, restored[i]); free(compressed); free(restored); return 1; } } } fprintf(stdout, "Data was restored OK\n"); free(compressed); free(restored); return 0; }