Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
main.cpp
Package: solitude.rar [view]
Upload User: szqxhk
Upload Date: 2010-02-15
Package Size: 54k
Code Size: 3k
Category:
Compress-Decompress algrithms
Development Platform:
Visual C++
- #include <windows.h>
- #include <stdio.h>
- #include <memory.h>
- #include "lz77.h"
- //////////////////////////////////////////////////////////////////
- // out file format:
- // 0;flag2;buffer;0;flag2;buffer;...flag1;flag2;bufferlast
- // flag1 - 2 bytes, source buffer block length
- // if block size is 65536, be zero
- // flag2 - 2 bytes, compressed buffer length
- // if can not compress, be same with flag1
- //////////////////////////////////////////////////////////////////
- void main(int argc, char* argv[])
- {
- if (argc != 4)
- {
- puts("Usage: ");
- printf(" Compress : %s c sourcefile destfilen", argv[0]);
- printf(" Decompress : %s d sourcefile destfilen", argv[0]);
- return;
- }
- BYTE soubuf[65536];
- BYTE destbuf[65536 + 16];
- FILE* in;
- FILE* out;
- in = fopen(argv[2], "rb");
- if (in == NULL)
- {
- puts("Can't open source file");
- return;
- }
- out = fopen(argv[3], "wb");
- if (out == NULL)
- {
- puts("Can't open dest file");
- fclose(in);
- return;
- }
- fseek(in, 0, SEEK_END);
- long soulen = ftell(in);
- fseek(in, 0, SEEK_SET);
- CCompressLZ77 cc;
- WORD flag1, flag2;
- if (argv[1][0] == 'c') // compress
- {
- int last = soulen, act;
- while ( last > 0 )
- {
- act = min(65536, last);
- fread(soubuf, act, 1, in);
- last -= act;
- if (act == 65536) // out 65536 bytes
- flag1 = 0;
- else // out last blocks
- flag1 = act;
- fwrite(&flag1, sizeof(WORD), 1, out);
- int destlen = cc.Compress((BYTE*)soubuf, act, (BYTE*)destbuf);
- if (destlen == 0) // can't compress the block
- {
- flag2 = flag1;
- fwrite(&flag2, sizeof(WORD), 1, out);
- fwrite(soubuf, act, 1, out);
- }
- else
- {
- flag2 = (WORD)destlen;
- fwrite(&flag2, sizeof(WORD), 1, out);
- fwrite(destbuf, destlen, 1, out);
- }
- }
- }
- else if (argv[1][0] == 'd') // decompress
- {
- int last = soulen, act;
- while (last > 0)
- {
- fread(&flag1, sizeof(WORD), 1, in);
- fread(&flag2, sizeof(WORD), 1, in);
- last -= 2 * sizeof(WORD);
- if (flag1 == 0)
- act = 65536;
- else
- act = flag1;
- last-= flag2 ? (flag2) : act;
- if (flag2 == flag1)
- {
- fread(soubuf, act, 1, in);
- }
- else
- {
- fread(destbuf, flag2, 1, in);
- if (!cc.Decompress((BYTE*)soubuf, act, (BYTE*)destbuf))
- {
- puts("Decompress error");
- fclose(in);
- fclose(out);
- return;
- }
- }
- fwrite((BYTE*)soubuf, act, 1, out);
- }
- }
- else
- {
- puts("Usage: ");
- printf(" Compress : %s c sourcefile destfilen", argv[0]);
- printf(" Decompress : %s d sourcefile destfilen", argv[0]);
- }
- fclose(in);
- fclose(out);
- }