Read the specified file content from the compressed package file

Some configurations: VC++ directory -> include directory && library directory: add F:\zlib-1.2.11\contrib\minizip (because the unzip file of the zlib library is used)
Linker -> Input: add zlibwapi.lib
Linker->General->Additional Library Directory: Add F:\zlib-1.2.11\contrib\vstudio\vc11\x64\ZlibDllDebug\ (this is the drive letter where I store zlib)
#include "stdio.h"
#include "unzip.h"
#include "string.h"




int main(int argc, char* argv[])

{
	int err;
	char szFileName[255];

	//declare the struct
	unz_global_info global_info;
	unz_file_info file_info;

	//open compressed file
	unzFile uzf = unzOpen64(argv[1]);

	// locate the specified file
	err = unzLocateFile(uzf, "classes.dex", 0);
	if (UNZ_OK != err)
	{
		printf("GetFileInZip unzLocateFile failed... error:%d\n");
		return err;
	}
	//Get the information of the currently selected internal compressed file
	err = unzGetCurrentFileInfo(uzf, &file_info, szFileName, sizeof(szFileName), NULL, 0, NULL, 0);

	if (UNZ_OK != err)
	{
		printf("unzGetCurrentFileInfo failed... error:%d\n", err);
		return err;
	}
	//Select to open the located file
	err = unzOpenCurrentFile(uzf);
	if (err != UNZ_OK)
	{
		printf("Failed to open the specified file!");
		return 0;
	}
	//read content
	int len = file_info.uncompressed_size;
	char * ptr_arr;
	ptr_arr = (char*)malloc(len); // allocate memory dynamically
	err = unzReadCurrentFile(uzf, ptr_arr, file_info.uncompressed_size);
	FILE* fp = fopen("./1.dex","wb+");
	fwrite(ptr_arr,1,len,fp); //Write 1 byte of content each time, and write len times in total
	if (err < 0)
	{
		printf("unzReadCurrentFile failed... error:%d\n", err);
	}
	//close the file
	unzCloseCurrentFile(uzf);
	unzClose(uzf);
	fclose(fp);
	return 0;

}

Some small errors encountered:

Problems with VS2015 compilation: add _CRT_SECURE_NO_WARNINGS to C/C++->Preprocessor->Preprocessor Definition

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893092&siteId=291194637