void QtRAR::close() { if (isOpen()) { RARCloseArchive(m_p->m_hArc); m_p->reset(); } }
/* --------------------------------------- 释放接口 --------------------------------------- */ static void_t iPAK_RAR_release ( __CR_IN__ iPACKAGE* that ) { leng_t idx; iPAK_RAR* real; sPAK_RAR_FILE* list; pack_free_list(that); real = (iPAK_RAR*)that; list = (sPAK_RAR_FILE*)real->pack.__filelst__; if (list != NULL) { for (idx = 0; idx < real->m_cnt; idx++) { mem_free(list[idx].base.find); mem_free(list[idx].base.name); } mem_free(list); } if (real->m_rar != NULL) RARCloseArchive(real->m_rar); TRY_FREE(real->m_ansi); TRY_FREE(real->m_wide); TRY_FREE(real->m_pass); mem_free(that); }
int ExtractFile(char* filename) { char* outputFolder = "temp\\"; HANDLE hData; INT RHCode; INT PFCode; RARHeaderData HeaderData; RAROpenArchiveDataEx OpenArchiveData = { 0 }; { OpenArchiveData.ArcName = filename; //OpenArchiveData.CmtBuf = NULL; //OpenArchiveData.CmtBufSize = 0; OpenArchiveData.OpenMode = RAR_OM_EXTRACT; } hData = RAROpenArchiveEx(&OpenArchiveData); if(OpenArchiveData.OpenResult != 0) { char buf[128]; memset(buf, 0, sizeof(buf)); sprintf_s(buf, "Error opening <%s>", filename); MessageBox(NULL, "Error!", buf, MB_OK|MB_ICONERROR); return -1; } RARSetPassword(hData, "8863da099df8adc830836744e19293ef"); while((RHCode = RARReadHeader(hData, &HeaderData)) == 0) { PFCode = RARProcessFile(hData, RAR_EXTRACT, outputFolder, NULL); if(PFCode != 0) { char buf[128]; memset(buf, 0, sizeof(buf)); sprintf_s(buf, "Error extracting <%s>", filename); MessageBox(NULL, "Error!", buf, MB_OK|MB_ICONERROR); return -1; } struct stat st; std::string file = outputFolder; file += HeaderData.FileName; stat(file.c_str(), &st); if(st.st_mode & S_IFREG) { vfs->patchfiles.push_back(file); } } if(RHCode == ERAR_BAD_DATA) { char buf[128]; memset(buf, 0, sizeof(buf)); sprintf_s(buf, "Error <%s> is corrupt", filename); MessageBox(NULL, "Error!", buf, MB_OK|MB_ICONERROR); return -1; } RARCloseArchive(hData); DeleteFile(filename); return 0; }
/* XXX a return value indicate the status */ static void extract_rar_file_into_loader(GdkPixbufLoader * loader, const char *archname, const char *archpath) { struct RAROpenArchiveData arcdata; int code = 0; int ret; HANDLE hrar; arcdata.ArcName = (char *)archname; arcdata.OpenMode = RAR_OM_EXTRACT; arcdata.CmtBuf = NULL; arcdata.CmtBufSize = 0; hrar = RAROpenArchive(&arcdata); if (hrar == NULL && loader == NULL) return; RARSetCallback(hrar, rarcbpixbuf, (LONG) loader); do { struct RARHeaderData header; if ((ret = RARReadHeader(hrar, &header)) != 0) { if (ret != ERAR_UNKNOWN && ret != ERAR_BAD_DATA) break; RARCloseArchive(hrar); //test_rar_file_password(buf, archname, archpath); return; } if (strcasecmp(header.FileName, archpath) == 0) { code = RARProcessFile(hrar, RAR_TEST, NULL, NULL); break; } } while (RARProcessFile(hrar, RAR_SKIP, NULL, NULL) == 0); RARCloseArchive(hrar); if (code == 22) { //test_rar_file_password(buf, archname, archpath); return; } if (code != 0) { g_object_unref(loader); } }
bool QtRARPrivate::reopen() { QtRAR::OpenMode lastOpenMode = m_mode; RARCloseArchive(m_hArc); m_curIndex = 0; m_error = ERAR_SUCCESS; m_hArc = 0; m_mode = QtRAR::OpenModeNotOpen; return m_q->open(lastOpenMode, m_password); }
int main(int argc, char* argv[]) { for (int i = 0; i < RSN_ENTRIES; i++) { rsnEntries[i] = NULL; rsnSize[i] = 0; } rsnInfo = NULL; resetRsnEntries(); printf("\nUnrar Into Memory Learn 1.0\n\n"); rarFileData.ArcName = "test.rar"; rarFileData.OpenMode = RAR_OM_EXTRACT; rarHandle = RAROpenArchive(&rarFileData); printf("Opening test.rar: %d\n", rarFileData.OpenResult); if (rarFileData.OpenResult == ERAR_SUCCESS) { printf("-->Success\n"); int res = RARReadHeader(rarHandle, &rarFileHeader); while(res == ERAR_SUCCESS) { printf("Header '%s' -> %d -> %d Bytes\n", rarFileHeader.FileName, res, rarFileHeader.UnpSize); if (strfind(rarFileHeader.FileName, ".spc") != NULL && rsnCount < RSN_ENTRIES && rarFileHeader.UnpSize > 66000 && rarFileHeader.UnpSize < 67000) { printf("--> SPC File, going to extract\n"); rsnEntries[rsnCount] = (char*)malloc(rarFileHeader.UnpSize); if (rsnEntries[rsnCount] != NULL) { printf("--> Pointer to memory: %p\n", rsnEntries[rsnCount]); RARSetCallback(rarHandle, rarExtractSpcBytes, (long)rsnEntries[rsnCount]); RARProcessFile(rarHandle, RAR_EXTRACT, NULL, NULL); rsnSize[rsnCount] = rarFileHeader.UnpSize; rsnCount++; } } else if (strfind(rarFileHeader.FileName, ".txt") != NULL && rsnInfo == NULL && rarFileHeader.UnpSize < 10000) { printf("--> TXT File, going to extract\n"); rsnInfo = (char*)calloc(rarFileHeader.UnpSize + 1, 1); if (rsnInfo != NULL) { RARSetCallback(rarHandle, rarExtractInfoBytes, (long)rsnInfo); RARProcessFile(rarHandle, RAR_EXTRACT, NULL, NULL); } } else { printf("--> Skip\n"); RARProcessFile(rarHandle, RAR_SKIP, NULL, NULL); } res = RARReadHeader(rarHandle, &rarFileHeader); } printf("Result: %d\n", res); } RARCloseArchive(rarHandle); printf("Tracks: %d\n", rsnCount); printf("Info: %d\n\n%s\n", rsnInfo == NULL ? 0 : strlen(rsnInfo), rsnInfo == NULL ? "<?>" : rsnInfo); return 0; }
void ComicBookRAR::closeRar(HANDLE rarFile, RAROpenArchiveDataEx *flags) { if (rarFile) RARCloseArchive(rarFile); #if defined(wxUSE_UNICODE) && !defined(__WXOSX__) delete[] flags->ArcNameW; #else delete[] flags->ArcName; #endif }
void YKZip::Extra(const YKString& srcFile, const YKString& desPath, const YK_CHAR* passWord /* = "" */) { #if YK_OS == YK_OS_WINDOWS_NT setlocale(LC_ALL, NULL); HANDLE hArcData; int RHCode,PFCode; char CmtBuf[16384]; struct RARHeaderDataEx HeaderData; struct RAROpenArchiveDataEx OpenArchiveData; memset(&HeaderData,0,sizeof(HeaderData)); memset(&OpenArchiveData,0,sizeof(OpenArchiveData)); YKBuffer<YK_WCHAR> buffSrc(srcFile.size()+1); std::wcscpy(buffSrc.Begin(), srcFile.c_str()); YKBuffer<YK_WCHAR> buffDes(desPath.size()+1); std::wcscpy(buffDes.Begin(), desPath.c_str()); OpenArchiveData.ArcNameW = buffSrc.Begin(); OpenArchiveData.CmtBuf=CmtBuf; OpenArchiveData.CmtBufSize=sizeof(CmtBuf); OpenArchiveData.OpenMode=RAR_OM_EXTRACT; OpenArchiveData.Callback=YK_NULL; OpenArchiveData.UserData=0; hArcData = RAROpenArchiveEx(&OpenArchiveData); if (OpenArchiveData.OpenResult!=0) return; RARSetPassword(hArcData, const_cast<YK_CHAR*>(passWord)); while ((RHCode=RARReadHeaderEx(hArcData,&HeaderData))==0) { PFCode = RARProcessFileW(hArcData,RAR_EXTRACT, buffDes.Begin(), NULL); } RARCloseArchive(hArcData); #endif // YKString strExtra = g_Application.GetCurModulePath(); // strExtra.Append(L"HaoZip\\HaoZipC.exe x ").Append(srcFile).Append(L" -ao -y -o").Append(desPath); // strExtra.Append(L" -p").Append(L"42F7AA9A-67E0-4CC7-9526-B2E8B358F179"); // system(strExtra.ToString().c_str()); // return; // HZIP hz = OpenZip(srcFile.c_str(), passWord); // ZIPENTRY ze; // GetZipItem(hz,-1,&ze); // int num = ze.index; // for (int i = 0; i < num; ++i) // { // GetZipItem(hz, i, &ze); // UnzipItem(hz,i, (desPath + ze.name).c_str()); // } // // CloseZip(hz); }
int unrar_test_password(const char * file, const char * pwd) { void * unrar; struct RARHeaderData headerdata; struct RAROpenArchiveData archivedata; int password_incorrect; password_incorrect = 0; memset(&headerdata, 0, sizeof(headerdata)); memset(&archivedata, 0, sizeof(archivedata)); archivedata.ArcName = (char *) file; archivedata.OpenMode = RAR_OM_EXTRACT; unrar = RAROpenArchive(&archivedata); if (!unrar || archivedata.OpenResult) return -2; RARSetPassword(unrar, (char *) pwd); RARSetCallback(unrar, _unrar_test_password_callback, (long) &password_incorrect); if (RARReadHeader(unrar, &headerdata) != 0) goto error; if (RARProcessFile(unrar, RAR_TEST, NULL, NULL) != 0) goto error; if (password_incorrect) goto error; RARCloseArchive(unrar); return 0; error: RARCloseArchive(unrar); return -1; }
int CSVPRarLib::ListRar(CString fnRarPath , CStringArray* szaFiles) { szaFiles->RemoveAll(); struct RAROpenArchiveDataEx ArchiveDataEx; memset(&ArchiveDataEx, 0, sizeof(ArchiveDataEx)); ArchiveDataEx.ArcNameW = (LPTSTR)(LPCTSTR)fnRarPath; char fnA[MAX_PATH]; if(wcstombs(fnA, fnRarPath, fnRarPath.GetLength()+1) == -1) fnA[0] = 0; ArchiveDataEx.ArcName = fnA; ArchiveDataEx.OpenMode = RAR_OM_EXTRACT; ArchiveDataEx.CmtBuf = 0; try{ HANDLE hrar = RAROpenArchiveEx(&ArchiveDataEx); if(hrar) { struct RARHeaderDataEx HeaderDataEx; HeaderDataEx.CmtBuf = NULL; while(RARReadHeaderEx(hrar, &HeaderDataEx) == 0) { if (HeaderDataEx.Method == 0x30) { CString subfn(HeaderDataEx.FileNameW); BOOL bAlreadyHaveTheSame = false; for(int i = 0; i < szaFiles->GetCount(); i++){ if(szaFiles->GetAt(i) == subfn){ bAlreadyHaveTheSame = true; break; } } if(!bAlreadyHaveTheSame) szaFiles->Add(subfn); } RARProcessFile(hrar, RAR_SKIP, NULL, NULL); } RARCloseArchive(hrar); } }catch (...) { return 0; } return szaFiles->GetCount(); }
/* all filelist->data should be g_free() * if filter == NULL, all files will be in the list */ GList *get_filelist_from_rar(const char *archname, GtkFileFilter * filter) { GList *filelist = NULL; gchar *filename = NULL; struct RAROpenArchiveData arcdata; int ret; HANDLE hrar; arcdata.ArcName = (char *)archname; arcdata.OpenMode = RAR_OM_EXTRACT; arcdata.CmtBuf = NULL; arcdata.CmtBufSize = 0; hrar = RAROpenArchive(&arcdata); if (hrar == NULL) return NULL; do { struct RARHeaderData header; if ((ret = RARReadHeader(hrar, &header)) != 0) { if (ret != ERAR_UNKNOWN && ret != ERAR_BAD_DATA) break; RARCloseArchive(hrar); //test_rar_file_password(buf, archname, archpath); return NULL; } filename = g_strdup(header.FileName); if ((filter != NULL && gtk_filename_filter(filename, filter)) || filter == NULL) filelist = g_list_append(filelist, filename); } while (RARProcessFile(hrar, RAR_SKIP, NULL, NULL) == 0); RARCloseArchive(hrar); return filelist; }
jint Java_de_illogical_modo_SpcDecoder_spcLoadRSN(JNIEnv* env, jclass clazz, jstring path) { char cpath[1024]; memset(cpath, 0, 1024); int clen = (*env)->GetStringLength(env, path); (*env)->GetStringUTFRegion(env, path, 0, clen, cpath); rarFileData.ArcName = cpath; rarFileData.OpenMode = RAR_OM_EXTRACT; rarHandle = RAROpenArchive(&rarFileData); if (rarFileData.OpenResult == ERAR_SUCCESS) { int res = RARReadHeader(rarHandle, &rarFileHeader); while(res == ERAR_SUCCESS) { if (strfind(rarFileHeader.FileName, ".spc") != NULL && rsnCount < RSN_ENTRIES && rarFileHeader.UnpSize > 66000 && rarFileHeader.UnpSize < 67000) { rsnEntries[rsnCount] = (char*)malloc(rarFileHeader.UnpSize); if (rsnEntries[rsnCount] != NULL) { rarExtractSpcBytes(0xFFFF, 0, 0, 0); RARSetCallback(rarHandle, rarExtractSpcBytes, (long)rsnEntries[rsnCount]); RARProcessFile(rarHandle, RAR_EXTRACT, NULL, NULL); rsnSize[rsnCount] = rarFileHeader.UnpSize; rsnCount++; } } else if (strfind(rarFileHeader.FileName, ".txt") != NULL && rsnInfo == NULL && rarFileHeader.UnpSize < 10000) { rsnInfo = (char*)calloc(rarFileHeader.UnpSize + 1, 1); if (rsnInfo != NULL) { rarExtractInfoBytes(0xFFFF, 0, 0, 0); RARSetCallback(rarHandle, rarExtractInfoBytes, (long)rsnInfo); RARProcessFile(rarHandle, RAR_EXTRACT, NULL, NULL); } } else { RARProcessFile(rarHandle, RAR_SKIP, NULL, NULL); } res = RARReadHeader(rarHandle, &rarFileHeader); } } RARCloseArchive(rarHandle); return rsnCount; }
////////////////////////////////////////////////////////// // // ExtractFiles // // Extract files from supplied archive filename // ////////////////////////////////////////////////////////// bool ExtractFiles ( const SString& strFile ) { // Open archive RAROpenArchiveData archiveData; memset ( &archiveData, 0, sizeof ( archiveData ) ); archiveData.ArcName = (char*)*strFile; archiveData.OpenMode = RAR_OM_EXTRACT; HANDLE hArcData = RAROpenArchive ( &archiveData ); if ( !hArcData ) return false; char* szPassword = "******"; RARSetPassword ( hArcData, szPassword ); bool bOk = false; // Do each file while ( true ) { // Read file header RARHeaderData headerData; memset ( &headerData, 0, sizeof ( headerData ) ); int iStatus = RARReadHeader ( hArcData, &headerData ); if ( iStatus != 0 ) { if ( iStatus == ERAR_END_ARCHIVE ) bOk = true; break; } // Read file data iStatus = RARProcessFile ( hArcData, RAR_EXTRACT, NULL, NULL ); if ( iStatus != 0 ) break; } RARCloseArchive ( hArcData ); return bOk; }
void UnPackIntoSubdirActionTask::process(const volatile Flags &aborted, QString &error) { QByteArray fileName; IFileContainer::Holder destination; Tryier tryier(this, &UnPackIntoSubdirActionTask::askForSkipIfNotCopy, aborted); int res; void *archive; struct RARHeaderDataEx fileInfo; struct RAROpenArchiveDataEx archiveData; m_aborted = &aborted; for (AsyncAction::FilesList::size_type i = 0, size = files().size(); i < size && !aborted; ++i) if (tryier.tryTo(CreateDestination(m_container, m_file = files().at(i).second, destination))) { fileName = m_container->location(m_file); memset(&archiveData, 0, sizeof(struct RAROpenArchiveDataEx)); archiveData.ArcName = fileName.data(); archiveData.OpenMode = RAR_OM_EXTRACT; if (archive = RAROpenArchiveEx(&archiveData)) { RARSetCallback(archive, callbackProc, (long)this); while ((res = RARReadHeaderEx(archive, &fileInfo)) == 0 && !aborted) { if ((res = RARProcessFile(archive, RAR_EXTRACT, const_cast<char *>(destination->location().as<QByteArray>().data()), NULL)) != 0) break; } if (!aborted && res != ERAR_END_ARCHIVE) error = Scanner::errorDescription(res); RARCloseArchive(archive); } } }
JNIEXPORT jbyteArray JNICALL Java_net_kervala_comicsreader_RarFile_nativeGetData(JNIEnv *env, jclass, jstring jFilename, jstring jEntry) { jbyteArray ret = NULL; const char *filename = env->GetStringUTFChars(jFilename, NULL); const char *entry = env->GetStringUTFChars(jEntry, NULL); RAROpenArchiveData data; memset(&data, 0, sizeof(RAROpenArchiveData)); data.ArcName = (char*)filename; data.OpenMode = RAR_OM_EXTRACT; HANDLE handle = RAROpenArchive(&data); if (handle && !data.OpenResult) { RARHeaderData header; memset(&header, 0, sizeof(RARHeaderData)); // process each entry while (RARReadHeader(handle, &header) == 0) { // check if we must process this entry if (strcmp(header.FileName, entry) == 0) { // set buffer related variables Buffer buffer(header.UnpSize); RARSetCallback(handle, callbackData, (LPARAM)&buffer); // don't use RAR_EXTRACT because files will be extracted in current directory int result = RARProcessFile(handle, RAR_TEST, NULL, NULL); if (result) { LOGE("Unable to process %s, error: %d", header.FileName, result); } else { // allocates a new Java buffer ret = env->NewByteArray(buffer.getSize()); // copy C++ buffer data to Java buffer env->SetByteArrayRegion(ret, 0, buffer.getSize(), (jbyte *)buffer.getAddress()); // exit if file was correctly extracted break; } } else { // skip this entry int result = RARProcessFile(handle, RAR_SKIP, NULL, NULL); if (result) { LOGE("Unable to skip %s, error: %d", header.FileName, result); } } } RARCloseArchive(handle); } else { displayError(data.OpenResult, filename); } // release UTF-8 strings env->ReleaseStringUTFChars(jEntry, entry); env->ReleaseStringUTFChars(jFilename, filename); return ret; }
JNIEXPORT jobjectArray JNICALL Java_net_kervala_comicsreader_RarFile_nativeGetEntries(JNIEnv *env, jclass, jstring jFilename) { jobjectArray ret = NULL; const char *filename = env->GetStringUTFChars(jFilename, NULL); RAROpenArchiveData data; memset(&data, 0, sizeof(RAROpenArchiveData)); data.ArcName = (char*)filename; data.OpenMode = RAR_OM_LIST; HANDLE handle = RAROpenArchive(&data); if (handle && !data.OpenResult) { RARHeaderData header; memset(&header, 0, sizeof(RARHeaderData)); Strings list; // read all entries while (RARReadHeader(handle, &header) == 0) { // add file to list only if not a directory if ((header.Flags & LHD_DIRECTORY) != LHD_DIRECTORY) list.addString(header.FileName); // skip entry content int result = RARProcessFile(handle, RAR_SKIP, NULL, NULL); if (result) { LOGE("Unable to process %s, error: %d", header.FileName, result); } } RARCloseArchive(handle); size_t count = list.size(); if (count > 0) { ret = (jobjectArray)env->NewObjectArray(count, env->FindClass("java/lang/String"), NULL); Strings *tmp = &list; int i = 0; while(tmp) { env->SetObjectArrayElement(ret, i++, env->NewStringUTF(tmp->getString())); tmp = tmp->getNext(); } } } else { displayError(data.OpenResult, filename); } env->ReleaseStringUTFChars(jFilename, filename); return ret; }
} void ImgBookRAR::closeRar(HANDLE rarFile, RAROpenArchiveDataEx *flags) { if (rarFile) RARCloseArchive(rarFile);
void ComicBookRAR::closeRar(HANDLE rarFile, RAROpenArchiveDataEx *flags) { if (rarFile) RARCloseArchive(rarFile); }
/* ======================================= RAR 文件读取 ======================================= */ CR_API sFMT_PRT* load_rar ( __CR_IO__ iDATIN* datin, __CR_IN__ const sLOADER* param ) { HANDLE rar; sARRAY list; int32u attr; sint_t retc; leng_t fpos; iPAK_RAR* port; sFMT_PRT* rett; sPAK_RAR_FILE temp; RARHeaderDataEx info; RAROpenArchiveDataEx open; /* 只支持磁盘文件 */ if (param->type != CR_LDR_ANSI && param->type != CR_LDR_WIDE) return (NULL); /* 列表模式打开 RAR 文件 */ struct_zero(&open, RAROpenArchiveDataEx); if (param->type == CR_LDR_ANSI) open.ArcName = (ansi_t*)param->name.ansi; else open.ArcNameW = (wide_t*)param->name.wide; open.OpenMode = RAR_OM_LIST; rar = RAROpenArchiveEx(&open); if (rar == NULL) return (NULL); if (param->aprm != NULL && *(byte_t*)param->aprm != 0x00) { RARSetPassword(rar, (ansi_t*)param->aprm); attr = PAK_FILE_ENC; } else { attr = 0; } /* 开始逐个文件读取信息并定位 */ array_initT(&list, sPAK_RAR_FILE); list.free = rar_free; struct_zero(&info, RARHeaderDataEx); for (fpos = 0; ; fpos++) { /* 读取一个文件记录头 */ retc = RARReadHeaderEx(rar, &info); if (retc == ERAR_END_ARCHIVE) break; if (retc != ERAR_SUCCESS) goto _failure1; /* 目录文件不加入列表 */ if (info.Flags & RHDF_DIRECTORY) { retc = RARProcessFile(rar, RAR_SKIP, NULL, NULL); if (retc != ERAR_SUCCESS) goto _failure1; continue; } /* 文件名统一使用 UTF-8 编码 */ struct_zero(&temp, sPAK_RAR_FILE); temp.base.name = local_to_utf8(param->page, info.FileName); if (temp.base.name == NULL) goto _failure1; /* 设置公用文件属性 (偏移没有实际用处) */ temp.base.skip = sizeof(sPAK_RAR_FILE); temp.base.attr = attr; temp.base.offs = 0; temp.base.pack = mk_size(info.PackSizeHigh, info.PackSize); temp.base.size = mk_size(info.UnpSizeHigh, info.UnpSize); if (info.Method != 0x30) temp.base.attr |= PAK_FILE_CMP; if (info.Flags & RHDF_ENCRYPTED) temp.base.attr |= PAK_FILE_ENC; else temp.base.attr &= ~PAK_FILE_ENC; switch (info.Method) { case 0x30: temp.base.memo = "Storing"; break; case 0x31: temp.base.memo = "Fastest compression"; break; case 0x32: temp.base.memo = "Fast compression"; break; case 0x33: temp.base.memo = "Normal compression"; break; case 0x34: temp.base.memo = "Good compression"; break; case 0x35: temp.base.memo = "Best compression"; break; default: temp.base.memo = "Unknown compression"; break; } /* 设置私有文件属性 */ temp.id = fpos; temp.crc32 = (int32u)(info.FileCRC); temp.fattr = (int32u)(info.FileAttr); temp.ftime = (int16u)(info.FileTime & 0xFFFF); temp.fdate = (int16u)(info.FileTime >> 16); temp.htype = (int32u)(info.HashType); mem_cpy(temp.hash, info.Hash, sizeof(temp.hash)); /* 文件信息压入列表 */ if (array_push_growT(&list, sPAK_RAR_FILE, &temp) == NULL) { mem_free(temp.base.name); goto _failure1; } /* 跳过当前已读文件 */ retc = RARProcessFile(rar, RAR_SKIP, NULL, NULL); if (retc != ERAR_SUCCESS && retc != ERAR_BAD_DATA) goto _failure1; } /* 固定一下列表大小 */ if (!array_no_growT(&list, sPAK_RAR_FILE)) goto _failure1; /* 生成读包接口对象 */ port = struct_new(iPAK_RAR); if (port == NULL) goto _failure1; /* 保存需要用到的参数 */ port->m_temp = NULL; if (attr == 0) { port->m_pass = NULL; } else { port->m_pass = str_dupA((ansi_t*)param->aprm); if (port->m_pass == NULL) goto _failure2; } if (param->type == CR_LDR_ANSI) { port->m_wide = NULL; port->m_ansi = str_dupA(param->name.ansi); if (port->m_ansi == NULL) goto _failure3; } else { port->m_ansi = NULL; port->m_wide = str_dupW(param->name.wide); if (port->m_wide == NULL) goto _failure3; } port->m_rar = NULL; port->m_cur = (leng_t)-1; port->m_cnt = array_get_sizeT(&list, sPAK_RAR_FILE); port->pack.__filelst__ = array_get_dataT(&list, sPAK_FILE); port->pack.__vptr__ = &s_pack_vtbl; if (!pack_init_list((iPACKAGE*)port, TRUE)) goto _failure4; RARCloseArchive(rar); /* 返回读取的文件数据 */ rett = struct_new(sFMT_PRT); if (rett == NULL) { iPAK_RAR_release((iPACKAGE*)port); return (NULL); } CR_NOUSE(datin); rett->type = CR_FMTZ_PRT; rett->port = (iPORT*)port; rett->more = "iPACKAGE"; rett->infor = "Roshal ARchive (RAR)"; return (rett); _failure4: TRY_FREE(port->m_ansi); TRY_FREE(port->m_wide); _failure3: TRY_FREE(port->m_pass); _failure2: mem_free(port); _failure1: array_freeT(&list, sPAK_RAR_FILE); RARCloseArchive(rar); return (NULL); }
/* --------------------------------------- 读取文件数据 --------------------------------------- */ static bool_t iPAK_RAR_getFileData ( __CR_IN__ iPACKAGE* that, __CR_OT__ sBUFFER* buff, __CR_IN__ int64u index, __CR_IN__ bool_t hash ) { sint_t rett; int64u size; void_t* data; iPAK_RAR* real; sPAK_RAR_FILE* item; RARHeaderDataEx info; RAROpenArchiveDataEx open; /* 定位文件索引 */ CR_NOUSE(hash); real = (iPAK_RAR*)that; if (index >= real->m_cnt) return (FALSE); item = (sPAK_RAR_FILE*)real->pack.__filelst__; item += (leng_t)index; /* 获取文件数据 (0大小文件分配1个字节) */ size = item->base.size; if (size == 0) { data = mem_malloc(1); if (data == NULL) return (FALSE); size = 1; *(byte_t*)data = 0x00; } else { real->m_temp = data = mem_malloc64(size); if (data == NULL) return (FALSE); /* RAR 只能顺序读取文件 */ if (real->m_rar == NULL || item->id < real->m_cur) { /* 需要重新打开封包 */ if (real->m_rar != NULL) { RARCloseArchive(real->m_rar); real->m_rar = NULL; } struct_zero(&open, RAROpenArchiveDataEx); if (real->m_ansi != NULL) open.ArcName = real->m_ansi; else open.ArcNameW = real->m_wide; open.OpenMode = RAR_OM_EXTRACT; open.Callback = rar_mem_copy; open.UserData = (LPARAM)(&real->m_temp); real->m_rar = RAROpenArchiveEx(&open); if (real->m_rar == NULL) goto _failure1; if (real->m_pass != NULL) RARSetPassword(real->m_rar, real->m_pass); real->m_cur = 0; } /* 定位到指定文件 */ struct_zero(&info, RARHeaderDataEx); while (real->m_cur != item->id) { rett = RARReadHeaderEx(real->m_rar, &info); if (rett != ERAR_SUCCESS) goto _failure2; rett = RARProcessFile(real->m_rar, RAR_SKIP, NULL, NULL); if (rett != ERAR_SUCCESS && rett != ERAR_BAD_DATA) goto _failure2; real->m_cur += 1; } /* 测试目标文件就不会有磁盘操作了 */ rett = RARReadHeaderEx(real->m_rar, &info); if (rett != ERAR_SUCCESS) goto _failure2; rett = RARProcessFile(real->m_rar, RAR_TEST, NULL, NULL); if (rett != ERAR_SUCCESS) goto _failure2; real->m_cur += 1; } /* 返回文件数据 */ return (buffer_init(buff, data, (leng_t)size, TRUE)); _failure2: RARCloseArchive(real->m_rar); real->m_rar = NULL; _failure1: real->m_cur = (leng_t)-1; mem_free(data); return (FALSE); }
void Scanner::scan(const ScanArguments &arguments, QString &error) const { struct RAROpenArchiveDataEx archiveData; memset(&archiveData, 0, sizeof(struct RAROpenArchiveDataEx)); archiveData.ArcName = const_cast<char *>(m_filePath.data()); archiveData.OpenMode = RAR_OM_LIST; if (void *archive = RAROpenArchiveEx(&archiveData)) { QMap<QString, SnapshotItem *> parents; IFileContainer::Holder container; struct RARHeaderDataEx fileInfo; SnapshotItem *parent; SnapshotItem *entry; QString fileName; Location fileNameLocation; const wchar_t *path; wchar_t *sep; int res; while ((res = RARReadHeaderEx(archive, &fileInfo)) == 0 && !arguments.aborted) { path = fileInfo.FileNameW; if ((sep = wcschr(const_cast<wchar_t *>(path), L'/')) != NULL) { (*sep) = 0; SnapshotItem *&p = parents[fileName = QString::fromWCharArray(path)]; (*sep) = L'/'; if (p == NULL) { fileNameLocation = Info::location(fileName); arguments.snapshot.insert(fileNameLocation, p = parent = new SnapshotItem(m_container, fileNameLocation, fileInfo, NULL)); } else parent = p; path = (++sep); while ((sep = wcschr(const_cast<wchar_t *>(path), L'/')) != NULL) { (*sep) = 0; if (entry = parent->find(fileName = QString::fromWCharArray(path))) parent = entry; else { parent->insert(fileName, entry = new SnapshotItem(m_container, Info::location(fileName), fileInfo, parent)); parent = entry; } (*sep) = L'/'; path = (++sep); } if (!(fileName = QString::fromWCharArray(path)).isEmpty() && parent->find(fileName) == NULL) { parent->insert(fileName, new SnapshotItem(m_container, Info::location(fileName), fileInfo, parent)); } } else { SnapshotItem *&p = parents[fileName = QString::fromWCharArray(path)]; if (p == NULL) { fileNameLocation = Info::location(fileName); arguments.snapshot.insert(fileNameLocation, p = new SnapshotItem(m_container, fileNameLocation, fileInfo, NULL)); } } RARProcessFile(archive, RAR_SKIP, NULL, NULL); } if (!arguments.aborted) if (res == ERAR_END_ARCHIVE) for (Snapshot::const_iterator i = arguments.snapshot.begin(), end = arguments.snapshot.end(); i != end; ++i) static_cast<SnapshotItem *>((*i).second)->populateInfo(); else error = errorDescription(res); RARCloseArchive(archive); } }
void extract(const std::string & archive_name, const std::string & dest_dir, const std::string & password) { bool already_extracted; #ifdef WINDOWS already_extracted = contains(extracted_files, archive_name, false); #else already_extracted = contains(extracted_files, archive_name, true); #endif if (already_extracted) return; fprintf(stdout, "Extracting %s to %s\n", archive_name.c_str(), dest_dir.c_str()); HANDLE archive; struct RAROpenArchiveData data; struct RARHeaderData header; if (!fileExists(archive_name)) return; if (!createDir(dest_dir)) return; data.ArcName = (char *)archive_name.c_str(); data.OpenMode = RAR_OM_EXTRACT; data.CmtBuf = NULL; data.CmtBufSize = 0; //fprintf(stdout, "Opening %s\n", argv[1]); archive = RAROpenArchive(&data); if (!archive || data.OpenResult != 0) { fprintf(stderr, "Couldn't open %s\n", archive_name.c_str()); return; } header.CmtBuf = NULL; //fprintf(stdout, "Clearing password\n"); RARSetPassword(archive, (char*)password.c_str()); MultipartInfo info; //fprintf(stdout, "Setting callback\n"); RARSetCallback(archive, &missing_file, (ssize_t)&info); //fprintf(stdout, "Starting to process file\n"); // the complete path to the file to be extracted std::string dest_path; start_timer(); while (!RARReadHeader(archive, &header)) { dest_path = dest_dir + PATH_SEP + header.FileName; if (pathExists(dest_path)) { fprintf(stderr, "Skpping %s\n", header.FileName); continue; } if (RARProcessFile(archive, RAR_EXTRACT, (char *)dest_dir.c_str(), NULL)) { /*if (!pathExists(dest_path)) { fprintf(stderr, "Couldn't create %s\n", dest_path.c_str()); break; }*/ if (elapsed(five_seconds)) { info.bytes_processed = 0; start_timer(); } } else { break; } // (bytes / (1024 * 1024) = MBytes // ms / 1000 = s // (bytes / (1024 * 1024) / (ms / 1000) = 0.00095367431640625 * bytes/ms //fprintf(stdout, "%d MB/s", (int)(0.00095367431640625 * (info.bytes_processed / calc_elapsed()))); } //fprintf(stdout, "Closing file\n"); RARCloseArchive(archive); extracted_files.push_back(archive_name); }
int _tmain(int argc, _TCHAR* argv[]) { HANDLE rar_handle = NULL; //std::wstring fn_rar = L"E:\\-=eMule=-\\1140746814_39741.rar"; //std::wstring fn_rar = L"E:\\-=eMule=-\\Bride.Flight.2008.Bluray.720p.AC3.x264-CHD_ÐÂÄﺽ°à\\B2_ÐÂÄﺽ°à.part1.rar"; //std::wstring fn_rar = L"D:\\xxxx.part1.rar"; std::wstring fn_rar = L"E:\\-=eMule=-\\Overheard.2009.REPACK.CN.DVDRip.Xvid-XTM\\sample\\xtm-overheard.repack-sample_stored.rar"; //std::wstring fn_rar = L"E:\\-=eMule=-\\Overheard.2009.REPACK.CN.DVDRip.Xvid-XTM\\sample\\xtm-overheard.repack-sample_s.part1.rar"; std::wstring tmp_dir = L"C:\\Temp\\"; struct RAROpenArchiveDataEx ArchiveDataEx; memset(&ArchiveDataEx, 0, sizeof(ArchiveDataEx)); ArchiveDataEx.ArcNameW = (wchar_t*)fn_rar.c_str(); //ArchiveDataEx.ArcName = "E:\\-=eMule=-\\1140746814_39741.rar"; ArchiveDataEx.OpenMode = RAR_OM_EXTRACT; ArchiveDataEx.CmtBuf = 0; rar_handle = RAROpenArchiveEx(&ArchiveDataEx); const long buffsize = 1000; char testbuff[buffsize]; if (rar_handle) { wprintf(L"RAROpenArchiveEx open successed\n"); struct RARHeaderDataEx HeaderDataEx; HeaderDataEx.CmtBuf = NULL; while (RARReadHeaderEx(rar_handle, &HeaderDataEx) == 0) { // Õý³£½âѹ¸ÃÎļþ unsigned long long filesize = ((unsigned long long)HeaderDataEx.UnpSizeHigh << 32) + HeaderDataEx.UnpSize; int err = 0; std::wstring unp_tmpfile = tmp_dir + HeaderDataEx.FileNameW; err = RARProcessFileW(rar_handle, RAR_EXTRACT, (wchar_t*)tmp_dir.c_str(), HeaderDataEx.FileNameW); wprintf(L"RARProcessFileW to %s return %d size %lld %x %x\n", unp_tmpfile.c_str(), err, filesize, HeaderDataEx.UnpVer, HeaderDataEx.Method); } RARCloseArchive(rar_handle); } rar_handle = RAROpenArchiveEx(&ArchiveDataEx); if (rar_handle) { wprintf(L"RAROpenArchiveEx open successed\n"); struct RARHeaderDataEx HeaderDataEx; HeaderDataEx.CmtBuf = NULL; while (RARReadHeaderEx(rar_handle, &HeaderDataEx) == 0) { unsigned long long filesize = ((unsigned long long)HeaderDataEx.UnpSizeHigh << 32) + HeaderDataEx.UnpSize; int err = 0; std::wstring unp_tmpfile = tmp_dir + HeaderDataEx.FileNameW; err = RARExtractChunkInit(rar_handle, HeaderDataEx.FileName); if (err != 0) { wprintf(L"RARExtractChunkInit return error %d\n", err); continue; } FILE* unp_filehandle = NULL; err = _wfopen_s(&unp_filehandle, unp_tmpfile.c_str(), L"rb"); if (err) { wprintf(L"open extracted file fail %d %d\n", err, unp_filehandle); continue; } // ˳Ðò²âÊÔ int iExtractRet = 0; unsigned long long fpos = 0; do { iExtractRet = RARExtractChunk(rar_handle, (char*)testbuff, buffsize); // Compare if (compare_filebinary(unp_filehandle, fpos, testbuff, iExtractRet, 0)) { wprintf(L"Sequence compare difference found at %lld for %d\n", fpos, buffsize); break; } //else // wprintf(L"Sequence compare is same %lld %d\n", fpos, iExtractRet); fpos += iExtractRet; } while(iExtractRet > 0); // Ëæ»ú²âÊÔ for (int i = 0; i < 100; i++) { unsigned long long ll_pos = rand() * filesize/RAND_MAX; RARExtractChunkSeek(rar_handle, ll_pos, SEEK_SET); RARExtractChunk(rar_handle, (char*)testbuff, buffsize); // Compare if (compare_filebinary(unp_filehandle, ll_pos, testbuff, iExtractRet, 0)) { wprintf(L"Random compare difference found at %lld\n", ll_pos); break; } //else // wprintf(L"Random compare is same %lld\n", ll_pos); } wprintf(L"RARExtractChunk test for %s finished\n", unp_tmpfile.c_str()); } RARCloseArchive(rar_handle); } wprintf(L"Test finished\n"); scanf_s("%d"); return 0; }
JNIEXPORT jint JNICALL Java_com_aroma_unrartool_Unrar_RarGetArchiveItems (JNIEnv *env, jobject obj, jstring fname) { char outbuf[255]; char *cmtBuff =NULL; jstring jstr; int len = env->GetStringLength( fname); env->GetStringUTFRegion( fname, 0, len, outbuf); LOGI("openning Archive: %s for listing\n", outbuf); LOGI("==========================\n"); RAROpenArchiveDataEx data; memset(&data, 0, sizeof(RAROpenArchiveDataEx)); cmtBuff=(char*)malloc(MAX_COMMENT_SIZE); //memset(&environment, 0, sizeof(Environment)); data.ArcName = (char*)outbuf;//filename; if(cmtBuff) { data.CmtBuf=cmtBuff; data.CmtBufSize=MAX_COMMENT_SIZE; } data.OpenMode = RAR_OM_LIST; HANDLE handle = RAROpenArchiveEx(&data); int itemscount=0; if (handle && !data.OpenResult) { /*environment.env=env; environment.obj=obj; RARSetCallback(handle,UnRarCallBack,(LPARAM)&environment);*/ /* Create a new string and overwrite the instance field */ if(data.Flags & MHD_SOLID) { LOGI("Archive is Solid"); if(archivesolid) env->SetBooleanField(obj, archivesolid , true); } if(data.Flags & MHD_LOCK) { LOGI("Archive is LOCKED"); if(archivelocked) env->SetBooleanField(obj, archivelocked , true); } if(data.Flags & MHD_AV) { LOGI("Archive is Signed"); if(archivesigned) env->SetBooleanField(obj, archivesigned , true); } if(data.Flags & MHD_PROTECT) { LOGI("recovery Record is present"); if(archiverecoveryRecord) env->SetBooleanField(obj, archiverecoveryRecord , true); } if(data.Flags & MHD_VOLUME) { LOGI("Archive is a Volume"); if(archivevolume) env->SetBooleanField(obj, archivevolume , true); } if(data.CmtState == 1) { LOGI("Archive Comment read completely"); jstring jstr = env->NewStringUTF( data.CmtBuf ); if (jstr == NULL) { LOGE("Unable to instanciate a new UTF String, may be an out of memory"); /* out of memory */ } if(archiveCmtfid) env->SetObjectField(obj, archiveCmtfid , jstr); if(archivecommentPresent) env->SetBooleanField(obj, archivecommentPresent , true); } else { switch(data.CmtState) { case 0: LOGI("Archive Comments not present"); if(archivecommentPresent) env->SetBooleanField(obj, archivecommentPresent , false); break; case ERAR_NO_MEMORY : LOGI("No enough memory to extract Archive Comments"); break; case ERAR_BAD_DATA : LOGI("Broken Archive Comments"); break; case ERAR_UNKNOWN_FORMAT: LOGI("Unknown Archive comment format"); break; case ERAR_SMALL_BUF: LOGI("Buffer too small,Archive comments not completely read"); break; } } RARHeaderDataEx header; memset(&header, 0, sizeof(RARHeaderDataEx)); int headererror=0 ; while (headererror=RARReadHeaderEx(handle, &header) == 0) { //RARSetPassword(handle,"mahm1985"); int result = RARProcessFile(handle, RAR_OM_LIST, NULL/*"/mnt/sdcard/unrartestfile"*/, NULL); if (result) { LOGE("Unable to process %s, error: %d", header.FileName, result); } else { //LOGI("Processing file: %s \n", header.FileName); } itemscount++; } if(headererror==ERAR_BAD_DATA) LOGI("RARReadHeaderEx returned ERAR_BAD_DATA"); if(headererror==ERAR_END_ARCHIVE) LOGI("RARReadHeaderEx returned ERAR_END_ARCHIVE"); RARCloseArchive(handle); } else { printf("Error Code:%d \n",data.OpenResult); displayError(data.OpenResult, outbuf); return -1; } //env->ReleaseStringUTFChars(param2, filename); LOGI("end of RarOpenArchive()"); if(cmtBuff) free(cmtBuff); return itemscount; }
JNIEXPORT jint JNICALL Java_com_aroma_unrartool_Unrar_RarOpenArchive (JNIEnv *env, jobject obj, jstring param2, jstring param3) { //const char *filename = env->GetStringUTFChars(param2, NULL); char outbuf[255],extrPath[255]; jstring jstr; int retresult=0; jclass cls = env->GetObjectClass(obj); jmethodID mid = env->GetMethodID(cls, "relayMessage", "(ILjava/lang/String;)V"); if(mid == NULL) LOGE("Error retrieving methodID for %s \n", "relayMessage()"); int len = env->GetStringLength( param2); env->GetStringUTFRegion( param2, 0, len, outbuf); len = env->GetStringLength( param3); env->GetStringUTFRegion( param3, 0, len, extrPath); LOGI("openning Archive: %s \n", outbuf); LOGI("==========================\n"); LOGI("Extracting to :%s\n",extrPath); LOGI("==========================\n"); RAROpenArchiveDataEx data; memset(&data, 0, sizeof(RAROpenArchiveDataEx)); memset(&environment, 0, sizeof(Environment)); data.ArcName = (char*)outbuf;//filename; data.OpenMode = RAR_OM_EXTRACT; HANDLE handle = RAROpenArchiveEx(&data); if (handle && !data.OpenResult) { environment.env=env; environment.obj=obj; RARSetCallback(handle,UnRarCallBack,(LPARAM)&environment); bool firstcheck=true; RARHeaderDataEx header; memset(&header, 0, sizeof(RARHeaderDataEx)); int headererror=0 ; while ((headererror=RARReadHeaderEx(handle, &header)) == 0) { if(mid != NULL) { jstr = env->NewStringUTF(header.FileName); if (jstr != NULL) { env->CallVoidMethod(obj, mid,0,jstr); env->DeleteLocalRef(jstr); } else LOGE("Unable to Create JString ,outofmemory error");/* out of memory */ } int result = RARProcessFile(handle, RAR_EXTRACT, extrPath/*"/mnt/sdcard/unrartestfile"*/, NULL); if(firstcheck && (data.Flags & MHD_VOLUME)) { LOGI("Archive is a Volume"); if(header.UnpVer >=29 && (data.Flags & MHD_FIRSTVOLUME)==0) { LOGE("unrar from the wrong Volume"); if(archivefirstVolume) env->SetBooleanField(obj, archivefirstVolume , false); } firstcheck=false; } if (result) { LOGE("Unable to process %s, error: %d", header.FileName, result); retresult=result; if(mid != NULL) { jstr = env->NewStringUTF(header.FileName); if (jstr != NULL) { env->CallVoidMethod(obj, mid,result,jstr); env->DeleteLocalRef(jstr); } else LOGE("Unable to Create JString ,outofmemory error");/* out of memory */ } switch(result) { case ERAR_BAD_DATA : break; case ERAR_BAD_ARCHIVE: break; case ERAR_UNKNOWN_FORMAT: break; case ERAR_EOPEN: break; case ERAR_ECREATE: break; case ERAR_ECLOSE: break; case ERAR_EREAD: break; case ERAR_EWRITE: break; } } else { //LOGI("Processing file: %s \n", header.FileName); /*if(mid != NULL) { jstr = env->NewStringUTF(header.FileName); if (jstr != NULL) { env->CallVoidMethod(obj, mid,0,jstr); env->DeleteLocalRef(jstr); } else LOGE("Unable to Create JString ,outofmemory error"); }*/ } } if(headererror==ERAR_BAD_DATA) { LOGI("RARReadHeaderEx returned ERAR_BAD_DATA"); retresult= headererror; } if(headererror==ERAR_END_ARCHIVE) LOGI("RARReadHeaderEx returned ERAR_END_ARCHIVE"); LOGI("RARReadHeaderEx returned %d",headererror); RARCloseArchive(handle); } else { printf("Error Code:%d \n",data.OpenResult); return displayError(data.OpenResult, outbuf); //return -1; } //env->ReleaseStringUTFChars(param2, filename); LOGI("end of RarOpenArchive()"); return retresult; }
wxRarInputStream::~wxRarInputStream() { RARCloseArchive(m_hRar); }
extern u32 fs_rar_to_menu(const char *rarfile, u32 icolor, u32 selicolor, u32 selrcolor, u32 selbcolor) { int fid; struct RAROpenArchiveData arcdata; struct RARHeaderDataEx header; int ret; HANDLE hrar; t_fs_filetype ft; t_win_menuitem item; if (menu_renew(&g_menu) == NULL) { return 0; } fid = freq_enter_hotzone(); arcdata.ArcName = (char *) rarfile; arcdata.OpenMode = RAR_OM_LIST; arcdata.CmtBuf = NULL; arcdata.CmtBufSize = 0; hrar = RAROpenArchive(&arcdata); if (hrar == 0) { freq_leave(fid); return 0; } add_parent_to_menu(g_menu, icolor, selicolor, selrcolor, selbcolor); do { char t[20]; if ((ret = RARReadHeaderEx(hrar, &header)) != 0) { if (ret != ERAR_UNKNOWN) break; RARCloseArchive(hrar); if ((hrar = reopen_rar_with_passwords(&arcdata)) == 0) break; if (RARReadHeaderEx(hrar, &header) != 0) break; } if (header.UnpSize == 0) continue; ft = fs_file_get_type(header.FileName); if (ft == fs_filetype_chm || ft == fs_filetype_zip || ft == fs_filetype_rar) continue; win_menuitem_new(&item); item.data = (void *) ft; if (header.Flags & 0x200) { char str[1024]; const u8 *uni; memset(str, 0, 1024); uni = (u8 *) header.FileNameW; charsets_utf32_conv(uni, sizeof(header.FileNameW), (u8 *) str, sizeof(str)); buffer_copy_string_len(item.compname, header.FileName, 256); filename_to_itemname(&item, str); } else { buffer_copy_string_len(item.compname, header.FileName, 256); filename_to_itemname(&item, header.FileName); } SPRINTF_S(t, "%u", (unsigned int) header.UnpSize); buffer_copy_string(item.shortname, t); item.selected = false; item.icolor = icolor; item.selicolor = selicolor; item.selrcolor = selrcolor; item.selbcolor = selbcolor; item.data3 = header.UnpSize; win_menu_add(g_menu, &item); } while (RARProcessFile(hrar, RAR_SKIP, NULL, NULL) == 0); RARCloseArchive(hrar); freq_leave(fid); return g_menu->size; }