dlltest.c
上传用户:zswatin
上传日期:2007-01-06
资源大小:440k
文件大小:3k
源码类别:

压缩解压

开发平台:

C/C++

  1. /*
  2. minibz2
  3. libbz2.dll test program.
  4. by Yoshioka Tsuneo(QWF00133@nifty.ne.jp/tsuneo-y@is.aist-nara.ac.jp)
  5. This file is Public Domain.
  6. welcome any email to me.
  7. usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]
  8. */
  9. #define BZ_IMPORT
  10. #include "bzlib.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #ifdef _WIN32
  14. #include <io.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <windows.h>
  18. static int BZ2DLLLoaded = 0;
  19. static HINSTANCE BZ2DLLhLib;
  20. int BZ2DLLLoadLibrary(void)
  21. {
  22. HINSTANCE hLib;
  23. if(BZ2DLLLoaded==1){return 0;}
  24. hLib=LoadLibrary("libbz2.dll");
  25. if(hLib == NULL){
  26. puts("Can't load libbz2.dll");
  27. return -1;
  28. }
  29. BZ2DLLLoaded=1;
  30. BZ2DLLhLib=hLib;
  31. bzlibVersion=GetProcAddress(hLib,"bzlibVersion");
  32. bzopen=GetProcAddress(hLib,"bzopen");
  33. bzdopen=GetProcAddress(hLib,"bzdopen");
  34. bzread=GetProcAddress(hLib,"bzread");
  35. bzwrite=GetProcAddress(hLib,"bzwrite");
  36. bzflush=GetProcAddress(hLib,"bzflush");
  37. bzclose=GetProcAddress(hLib,"bzclose");
  38. bzerror=GetProcAddress(hLib,"bzerror");
  39. return 0;
  40. }
  41. int BZ2DLLFreeLibrary(void)
  42. {
  43. if(BZ2DLLLoaded==0){return 0;}
  44. FreeLibrary(BZ2DLLhLib);
  45. BZ2DLLLoaded=0;
  46. }
  47. #endif /* WIN32 */
  48. void usage(void)
  49. {
  50. puts("usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]");
  51. }
  52. void main(int argc,char *argv[])
  53. {
  54. int decompress = 0;
  55. int level = 9;
  56. char *fn_r,*fn_w;
  57. #ifdef _WIN32
  58. if(BZ2DLLLoadLibrary()<0){
  59. puts("can't load dll");
  60. exit(1);
  61. }
  62. #endif
  63. while(++argv,--argc){
  64. if(**argv =='-' || **argv=='/'){
  65. char *p;
  66. for(p=*argv+1;*p;p++){
  67. if(*p=='d'){
  68. decompress = 1;
  69. }else if('1'<=*p && *p<='9'){
  70. level = *p - '0';
  71. }else{
  72. usage();
  73. exit(1);
  74. }
  75. }
  76. }else{
  77. break;
  78. }
  79. }
  80. if(argc>=1){
  81. fn_r = *argv;
  82. argc--;argv++;
  83. }else{
  84. fn_r = NULL;
  85. }
  86. if(argc>=1){
  87. fn_w = *argv;
  88. argc--;argv++;
  89. }else{
  90. fn_w = NULL;
  91. }
  92. {
  93. int len;
  94. char buff[0x1000];
  95. char mode[10];
  96. if(decompress){
  97. BZFILE *BZ2fp_r;
  98. FILE *fp_w;
  99. if(fn_w){
  100. if((fp_w = fopen(fn_w,"wb"))==NULL){
  101. printf("can't open [%s]n",fn_w);
  102. perror("reason:");
  103. exit(1);
  104. }
  105. }else{
  106. fp_w = stdout;
  107. }
  108. if((BZ2fp_r == NULL && (BZ2fp_r = bzdopen(fileno(stdin),"rb"))==NULL)
  109. || (BZ2fp_r != NULL && (BZ2fp_r = bzopen(fn_r,"rb"))==NULL)){
  110. printf("can't bz2openstreamn");
  111. exit(1);
  112. }
  113. while((len=bzread(BZ2fp_r,buff,0x1000))>0){
  114. fwrite(buff,1,len,fp_w);
  115. }
  116. bzclose(BZ2fp_r);
  117. if(fp_w != stdout) fclose(fp_w);
  118. }else{
  119. BZFILE *BZ2fp_w;
  120. FILE *fp_r;
  121. if(fn_r){
  122. if((fp_r = fopen(fn_r,"rb"))==NULL){
  123. printf("can't open [%s]n",fn_r);
  124. perror("reason:");
  125. exit(1);
  126. }
  127. }else{
  128. fp_r = stdin;
  129. }
  130. mode[0]='w';
  131. mode[1] = '0' + level;
  132. mode[2] = '';
  133. if((fn_w == NULL && (BZ2fp_w = bzdopen(fileno(stdout),mode))==NULL)
  134. || (fn_w !=NULL && (BZ2fp_w = bzopen(fn_w,mode))==NULL)){
  135. printf("can't bz2openstreamn");
  136. exit(1);
  137. }
  138. while((len=fread(buff,1,0x1000,fp_r))>0){
  139. bzwrite(BZ2fp_w,buff,len);
  140. }
  141. bzclose(BZ2fp_w);
  142. if(fp_r!=stdin)fclose(fp_r);
  143. }
  144. }
  145. #ifdef _WIN32
  146. BZ2DLLFreeLibrary();
  147. #endif
  148. }