home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 June
/
VPR0106A.BIN
/
OLS
/
BZ2L003
/
bz2l003.lzh
/
BZ2LIB
/
MINIBZ2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1998-06-02
|
2KB
|
117 lines
#ifdef _WIN32
#include "bz2dll.h"
#else
#include "bz2lib.h"
#endif
#include <stdio.h>
#include <stdlib.h>
void usage(void)
{
puts("usage: minibz2 [-d] [-{1,2,..9}]");
}
void main(int argc,char *argv[])
{
int decompress = 0;
int level = 9;
char *fn_r,*fn_w;
#ifdef _WIN32
if(BZ2DLLLoadLibrary()<0){
puts("can't load dll");
exit(1);
}
#endif
while(++argv,--argc){
if(**argv =='-'){
char *p;
for(p=*argv+1;*p;p++){
if(*p=='d'){
decompress = 1;
}else if('1'<=*p && *p<='9'){
level = *p - '0';
}else{
usage();
exit(1);
}
}
}else{
break;
}
}
if(argc>=1){
fn_r = *argv;
argc--;argv++;
}else{
fn_r = NULL;
}
if(argc>=1){
fn_w = *argv;
argc--;argv++;
}else{
fn_w = NULL;
}
{
int len;
char buff[0x1000];
char mode[10];
if(decompress){
BZ2FILE *BZ2fp_r;
FILE *fp_w;
if(fn_w){
if((fp_w = fopen(fn_w,"wb"))==NULL){
printf("can't open [%s]\n",fn_w);
perror("reason:");
exit(1);
}
}else{
fp_w = stdout;
}
if((BZ2fp_r = BZ2Open(fn_r,"r"))==NULL){
printf("can't bz2openstream\n");
exit(1);
}
while((len=BZ2Read(BZ2fp_r,buff,0x1000))>0){
fwrite(buff,1,len,fp_w);
}
BZ2Close(BZ2fp_r);
if(fp_w != stdout) fclose(fp_w);
}else{
BZ2FILE *BZ2fp_w;
FILE *fp_r;
if(fn_r){
if((fp_r = fopen(fn_r,"rb"))==NULL){
printf("can't open [%s]\n",fn_r);
perror("reason:");
exit(1);
}
}else{
fp_r = stdin;
}
mode[0]='w';
mode[1] = '0' + level;
mode[2] = '\0';
if((BZ2fp_w = BZ2Open(fn_w,mode))==NULL){
printf("can't bz2openstream\n");
exit(1);
}
while((len=fread(buff,1,0x1000,fp_r))>0){
BZ2Write(BZ2fp_w,buff,len);
}
BZ2Close(BZ2fp_w);
if(fp_r!=stdin)fclose(fp_r);
}
}
#ifdef _WIN32
BZ2DLLFreeLibrary();
#endif
}