📄 example.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 * ************************************************************************** */#include <stdio.h>#include <stdlib.h>#include "urarlib.h" /* include the URARFileLib *//* Define the .RAR and the password here since it's easier to change it this*//* way. */#define _RAR_ARCHIVE_ "../rars/best.rar"#define _RAR_PASSWORD_ "password"void print_list(ArchiveList_struct *list); /* functions used within example*/void write_file(char *filename, char *data, unsigned long datasize);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 */ printf("\n Demonstration of the UniquE RAR File Lib\n"); printf( " ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~\n"); /*** 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\n"); printf(" *** note that the decompression of last file must ***\n"); printf(" *** fail since it doesn't exist in the sample RAR ***\n"); printf("\n\n"); } printf("\n (C) 2000-2001 by Christian Scheurer aka. UniquE of Vantage"); printf("\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", (int) list->item.UnpSize); printf("%8d", (int) list->item.PackSize); printf("%5d", (int)list->item.HostOS); printf("%10x", (int)list->item.FileTime); printf("%10x", (int)list->item.FileCRC); printf("%5d", (int)list->item.FileAttr); printf("%4d", (int)list->item.UnpVer); printf("%5d", (int)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"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -