⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.c

📁 一个解RAR文件的开发库源码
💻 C
字号:
/* *************************************************************************** * Program    : example.c * Function   : Example for the usage of the UniquE RAR File Library *
 *              *************************************************************
 *              *ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*
 *              *o NOTE: read urarlib.c for more details and use urarlib.h o*
 *              *o to configure the lib.                                   o* *              *ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*
 *              *************************************************************
 *
 * Author     : Christian Scheurer aka. UniquE (unique@vantage.ch) *              http://www.mountpoint.ch/unique * Version    :  * 0.1    20.08.2000    UnQ    initial version * 0.2    23.08.2000    UnQ    debug system is now part of the URARFileLib
 * 0.3    24.08.2000    UnQ&JW some minor changes
 * 0.4    21.11.2000    UnQ    rewrite of the example, now with list function
 * 0.5    18.03.2001    UnQ    example extended for memory-to-memory 
 *                             decompression.  * ************************************************************************** */#include <stdio.h>#include <stdlib.h>#include <urarlib.h>                        /* include the URARFileLib      */#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
  #include <windows.h>
  #include "resource.h"                     /* include the resources if you */
                                            /* want to link RAR files into  */
                                            /* the .EXE using MS VisualC    */
  #define _RAR_ARCHIVE_ &rarfile
/* Define the .RAR and the password here since it's easier to change it this*//* way.                                                                     */
#else  #define _RAR_ARCHIVE_ "../rars/best.rar"
#endif
#define _RAR_PASSWORD_ "password"

void print_list(ArchiveList_struct *list);  /* functions used within example*/
void write_file(char *filename, char *data, unsigned long datasize);
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
void openRARResource(MemoryFile *memfile, int resource_id);
#endif
int main (){  char            *data_ptr;                /* pointers for the decompressed*/
                                            /* data                         */
  unsigned long   data_size;                /* size of the decompressed     */
                                            /* data                         */
  int             ch;                       /* for "please press ENTER"     */
  int             filecounter = 0;          /* used for urarlib_list()      */  ArchiveList_struct *List = NULL;          /* structure with file infos    */
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
  MemoryFile      rarfile;                  /* file in memory, used for mem-*/
                                            /* to-mem decompression         */
#endif
  printf("\n Demonstration of the UniquE RAR File Lib\n");  printf(  " ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~\n");
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
  /*** "open" memory file                                                   */
  openRARResource(&rarfile, IDR_RARFILE_BEST_RAR);
#endif

  /*** list files from RAR archive                                          */
  printf("\n  o list all files from 'best.rar...");
  filecounter = urarlib_list(_RAR_ARCHIVE_, (ArchiveList_struct*)&List);

  printf("%d files(s) found.\n\n", filecounter);
  print_list(List);


  /*** extract files for RAR archive                                        */  printf("  o extracting 'best.rar:\\somedir\\myadlist.cfg'...");  if(urarlib_get(&data_ptr, &data_size, "somedir\\myadlist.cfg",                  _RAR_ARCHIVE_, _RAR_PASSWORD_))   {
    printf("OK.\n"); 
    write_file("file_1.bin", data_ptr, data_size);
    if(data_ptr != NULL) free(data_ptr);    /* free memory                  */
  } else 
  { 
    printf("Error!\n"); 
  }



  printf("  o extracting 'best.rar:\\cryptdir\\file_id.diz'...");
  if(urarlib_get(&data_ptr, &data_size, "cryptdir\\file_id.diz", 
                 _RAR_ARCHIVE_, _RAR_PASSWORD_)) 
  {
    printf("OK.\n"); 
    write_file("file_2.bin", data_ptr, data_size);
    if(data_ptr != NULL) free(data_ptr);    /* free memory                  */
  } else 
  { 
    printf("Error!\n"); 
  }


  printf("  o extracting 'best.rar:\\sound\\allmix.doc'...");
  if(urarlib_get(&data_ptr, &data_size, "sound\\allmix.doc", 
                 _RAR_ARCHIVE_, _RAR_PASSWORD_)) 
  {
    printf("OK.\n"); 
    write_file("file_3.bin", data_ptr, data_size);
    if(data_ptr != NULL) free(data_ptr);    /* free memory                  */
  } else 
  { 
    printf("Error!\n"); 
    printf("\n *** note that the decompression of last file must  ***"\
           "\n *** fail since it doesn't exist in the sample RAR  ***"\
           "\n\n");
  }

  printf("\n (C) 2000-2001 by Christian Scheurer aka. UniquE of Vantage"\
         "\n http://www.mountpoint.ch/unique and http://www.vantage.ch\n\n");
#ifdef _UNIX
  printf("Linux port by Johannes Winkelmann (jw@tks6.net)\n\n");
#endif
  printf("\n End of decompression. Please press [ENTER]...");
  ch = getchar();
  printf("\n");

  return 0;}


void print_list(ArchiveList_struct *list)
/* display structure of information about a RAR archive                     */
{
  int  ch;

                                            /* sorry for this ugly code, but*/
                                            /* it's better for printing.    */
  printf("                   Name     Size  Packed   OS  FileTime    ");
  printf("CRC-32 Attr Ver Meth\n");
  printf("     ------------------------------------------------------");
  printf("--------------------\n");

  while(list != NULL){
    if(list->item.NameSize < 23)
    {
      printf("%23s", list->item.Name);
    } else
    {
      printf("%23s", "[name too long]");
    }
    printf("%9d",  list->item.UnpSize);
    printf("%8d",  list->item.PackSize);
    printf("%5d",  list->item.HostOS);
    printf("%10x", list->item.FileTime);
    printf("%10x", list->item.FileCRC);
    printf("%5d",  list->item.FileAttr);
    printf("%4d",  list->item.UnpVer);
    printf("%5d",  list->item.Method);
    printf("\n");

    (ArchiveList_struct*)list = (ArchiveList_struct*)list->next;
  }

  printf("\n End of listing. Please press [ENTER]...");
  ch = getchar();
  printf("\n\n");
}



void write_file(char *filename, char *data, unsigned long datasize)
/* write buffer to a file                                                   */
{
  FILE            *datafp;

  if(((datafp = fopen(filename, "wb")) == NULL) || (data == NULL))
  {
    printf("  o Error creating binary output file!\n\n");
  } else
  { 
    printf("  o writing %u bytes to '%s'...",
         (unsigned int)datasize, filename);
    fwrite(data, 1, datasize, datafp);
    fclose(datafp);
    printf("OK.\n\n");
  }
}


#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION

void openRARResource(MemoryFile *memfile, int resource_id)
/* get pointer to RAR file in memory which is linked as resource file       */
/* This works fine for MS VisualC 6.0, you may have to change this method   */
/* for other compilers and operating systems. Please give me your versions  */
/* for gcc, BC and other compilers, so I can distribute them with the next  */
/* release.                                                                 */
{
  HRSRC           hsResourceFH;             /* needed to load resources     */
  HGLOBAL         hgResourceLH;
  HINSTANCE	      hInstance  = GetModuleHandleA(NULL); 

  memfile->data   = 0;                      /* init with NULL values        */
  memfile->size   = 0;
  memfile->offset = 0;

  hsResourceFH = FindResource(hInstance, MAKEINTRESOURCE(resource_id), "RARFILE");
  if(hsResourceFH)
  {
    hgResourceLH = LoadResource(hInstance, hsResourceFH);
    if(hgResourceLH)
    {
       memfile->size = SizeofResource(hInstance, hsResourceFH);
       memfile->data = (void*)LockResource(hgResourceLH);
    }
  } 

}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -