Unzip tar.gz file under Windows

Source: Internet
Author: User
Tags uncompress

A recent project, using gadgets instead of manually submitting to the site and getting results.

A problem was encountered during the development process.

The format returned by the Web site is a tar.gz file. The first way to think about it is to use a third-party tool directly, using 7z. However, the system used by the user is win10 and does not run as an administrator without the right to call third-party tools. It was decided to use the library to solve the problem.

Here are some of the issues that are documented under Windows decompression tar.gz.

In fact, tar.gz is a two-time package.

The first is the TAR package (this will not compress, the file will be larger), and the second is the GZ file's algorithm compression.

The unpacked GZ file is using the Zlib library.

Let's talk about the use of zlib.

At first, I was lazy. Download the version of Windows develop directly from the official website, including header files, static libraries, and DLLs. However, there was a lack of zlib1.dll during the operation. After repeated toss, no solution. He this library generation time is 2005, may be running under the new version of Windows will have the problem.

I know the system is not much, did not find deep-seated reasons. If there is a great God know the principle please inform.

After that, a new version is downloaded and compiled. The specific compilation process has forgotten, quite smooth. After executing a command in the VS command line, the VS project is generated directly. Select either a static library or a dynamic library to compile.

I do not know whether there can send CSDN connection, I was based on this article to compile: http://blog.csdn.net/shellching/article/details/8116622.

Of course, the version I used was vs2015. This method can also be passed smoothly.

The next step is the decompression. The information I found on the Internet at the beginning was all compress and uncompress, and later found that:

Wrong!

It's not that much of a decompression.

The specific reason is not understood in detail, in general my understanding is, in fact, the GZ file needs a header to identify the compression level and other information. and compress and uncompress just compress the byte stream, simply call Uncompress without knowing the starting position of the data and the compression information of the data, it is unable to compress normally.

Here is the decompression code:

Initialize the structure of the body

Z_stream STRM = {0};

Strm.zalloc = Z_null;

Strm.zfree = Z_null;
Strm.opaque = Z_null;

The source data, source data length, target data length, and destination data address are initialized separately below

strm.next_in = Srcbuffer;
strm.avail_in = Srclength;
Strm.avail_out = Dstlength;
Strm.next_out = Dstbuffer;

The second parameter of the following function is in the function description of the header file, where this parameter means that the information header is automatically judged
int res = INFLATEINIT2 (&STRM, max_wbits + 32);

Perform decompression

res = inflate (&STRM, Z_no_flush);

Recycling Resources
Inflateend (&STRM);

The above code I removed the results of the judgment of the part, the specific results of judging everyone refer to the information of the header file. I actually did not make a detailed judgment in the code, but simply judged the success failure, and did not return the reason. We're not going to put up a hole here.

There are a few things to note:

1, the target memory length needs to be sufficient. Of course not enough, it does not matter, the function will find and return this error at execution, and gradually increase the buffer after judgment.

2, the length of the target data after successful conversion is: Dstlen-strm.avail_out.

3, If you compile and use these functions, the program will somehow collapse .

4, if you are a novice (now there is no novice on vs with this ... ), you may be able to report errors that cannot be connected, of course, you introduced Lib or will be reported.

First of all, why would you report a connection error, need to add a macro zlib_winapi, if there is a novice looking at, share with you.

A library, whether static or dynamic. When there is a header file, there are two places to note, one is that when you compile the library, the export function is preceded by a plus export, the second is when you lead the storage, the export function is not the import before.

Some libraries because to deal with cross-platform issues, the library's macro definition of the source code is very complex, this time to carefully observe, set the response of the macro to determine the correct introduction and export of library functions, if you use VS, the VS will be based on the state of the macro definition to highlight the specific code to use which macro.

Be aware of when compiling and introducing.

Then, after the crash, a variety of Google reasons, but never find out why, because there was no decompression success, I also once suspected that my code is a problem, or library problems.

Here, thank a great God: http://blog.csdn.net/u013283835/article/details/70311499

Really kneeling thanks. How did this come to be found. I hope you can fix it to Sun Yi-jin.

According to the great God, the asminf in the preprocessor is removed from the macro. The execution program writes the converted byte stream to the file. It's done.

Here despise the next csdn: My csdn because a few years ago to others hair QSs code was blocked. I am also drunk, send a QSS code can be sealed. It's also a technology forum. Oh.

The tar file is then parsed. The first thought of using the Libarchive library.

Well. Compile, connect, import, use. After all the setbacks, I suddenly found that: Libarchive does not support the full path, can only open the current working directory of the file, of course, it may be my configuration is not well-equipped, but the libarchive is too little information.

The official documents are also only very sketchy explanations. And under the WIN10, if the program is installed in the C-drive program files, running programs do not create file permissions, in other disks you need to constantly set the current path. Give. This library is really not very difficult to use.

Check the format of the tar file:

Http://blog.chinaunix.net/uid-20357359-id-1963469.html

Https://www.gnu.org/software/tar/manual/html_node/Standard.html

Well, it looks like a simple unpacking is not complicated. Plus the tar files in my little project are automatically generated, only a single level directory. Solve the problem yourself.

Here's the code:

typedef struct POSIX_HEADER

{
Char name[100];
Char Mode[8];
Char Uid[8];
Char Gid[8];
Char size[12];
Char mtime[12];
Char Chksum[8];
Char Typeflag;
Char linkname[100];
Char magic[6];
Char version[2];
Char uname[32];
Char gname[32];
Char Devmajor[8];
Char Devminor[8];
Char prefix[155];
}tar_header;

//parameter is target path, tar buffer, buffer length
voidExtracttar (Const Char*dstdir, BYTE *buffer, ULONGF len) {ULONGF Index=0; while(true) { if(Index = = Len-1) { return; } tar_header Newheader= {0 }; memcpy (&newheader, buffer + index,sizeof(Tar_header)); Index+=sizeof(Tar_header); if(strcmp (Newheader.name,"") ==0) { return; } CharFilefullpath[max_path] = {0 }; memcpy (Filefullpath, Dstdir, strlen (Dstdir)); memcpy (Filefullpath+strlen (Dstdir), Newheader.name, strlen (newheader.name)); FILE*newfile =NULL; Fopen_s (&newfile, Filefullpath,"wb+");LongSize = Strtol (Newheader.size, (Char* *) (& (Newheader.size) + A),8); size_t Res= fwrite (buffer + index,1, size, newFile); Fclose (newFile);Index+=size;

padding byte blocksintNblock = index/ ++1; Index= Nblock * +; }}

Attention:

1, I still removed the return judgment of the code, if you want to complete the robust code, determine the various status return values.

2, this code can not unzip the containing folder or multi-level directory of tar files, and can not extract special files, and do not judge the byte offset data.

Simply put, this code only guarantees the needs of my small project. And to a large extent cannot satisfy other needs.

Through the above two steps, basically meet my needs. Here to share with you.

Unzip tar.gz file under Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.