How to get the original file extension after INFLATE? - c++

I'm using zlib and C++ to compress/decompress files.
I can now deflate any file and put it to .z extension. But when inflating .z file I have to manually put the needed extension. Inflating works as it should and I can easily inflate .z file to stdout or file.
I 've read zlib manual but couldn't manage, how to solve my problem. I suppose I need to dig the archive's header for this information?
Any help would be appreciated, thanks!

The zlib library will compress and decompress both zlib and gzip streams. zlib streams have very compact header and trailer with no provision to store file name information. The gzip header and trailer on the other hand can store a file name, which can be used when decompressing. You should read the zlib documentation to see how to use the gzip format, and how to process the gzip header.

Related

Compress a folder into a single file using lz4 command line

I need to compress the all of the contents of a directory into a single lz4 archive, i couldn't figure out a way to do that. I already have read all the available parameters, still no success. Kindly suggest me how can i achieve this.
Things like lz4, gzip, bzip2, and xz simply compress a stream of bytes. You need another utility, like tar, to convert a set of directories and files into a stream of bytes. The output of tar is then fed to the compressor. That's why you see archives with names like tar.gz or tar.xz.
How to use tar with lz4?

How can I compress multiple files using glib.zlibcompressor in vala?

I have seen the example in valadoc.com for one file, but is any way to compress a list of files into only compressed package?
Thanks.
GZip does not support doing that. You probably want to make a tar archive which is then compressed with gzip. In that case, use libarchive, for which there are Vala bindings.

how do i add/remove specific file in zip file with zlib?

how do i add/remove specific file in zip file with zlib?
there are data.zip.
it has 1.bmp and 2.bmp.
T want to add 3.bmp and remove 2.bmp.
how do i do?
Except deleting data.zip and compress 1/3.bmp to data.zip.
zlib does not support ZIP files by itself, only DEFLATE streams. ZIP uses DEFLATE, but it is not the same. You need a library such as libzip, zziplib, minizip, etc which support ZIP files, most of which in turn use zlib.

Using zlib to compress a directory

I have a C++ application that requires using a standard compression format for a directory.
I thought about using zip format.
And therefore, zlib was obvious.
My problem is building the dictionary, i.e. compressing a directory containing some files with zlib to a standard zip file.
I have read that zlib does it but I don't understand how. I've checked minzip code. It seems to use gzip. I could build my own format but I want to be able to open these files using 7z for debugging (just in case).
What should I use in zlib to compress a directory?
[edit]
In minzip
I used minzip to compress 1 file into its gz version -> not zip but fine to me. (I will have that on various compilers in the world -- having a standard format is easier in case there is an issue on the client's platform)
Also, there is this code in main. It loops on a list of files and writes it to an output.
But where is the information on the file location in the archive?
do {
if (uncompr) {
if (copyout) {
file = gzopen(*argv, "rb");
if (file == NULL)
fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
else
gz_uncompress(file, stdout);
} else {
file_uncompress(*argv);
}
} else {
if (copyout) {
FILE * in = fopen(*argv, "rb");
if (in == NULL) {
perror(*argv);
} else {
file = gzdopen(fileno(stdout), outmode);
if (file == NULL) error("can't gzdopen stdout");
gz_compress(in, file);
}
} else {
file_compress(*argv, outmode);
}
}
} while (argv++, --argc);
Sorry if this is obvious.
Look at minizip.c for an example of how to use minizip to create zip files. minizip is in the contrib directory of the zlib distribution, which means that it is third-party contributed code. It is not part of the zlib library.
Note that gzip is not zip. The zlib format is not zip. Raw deflate data is not zip. Those are all different formats. Raw deflate data is one of the compressed formats permitted within the zip format, and in fact is the most common. zlib is useful for processing zip files, but it does not do that by itself. zlib by itself provides only the compression and decompression engines and CRC calculation.
Do not mix together zip - standard for archive, which can containe multiple files in it, and zlib - which is used in zip to compress the single file/data stream. To compress a directory you should use minizip/infozip or any other library which is compatible with ZIP archive format.
I was misunderstanding a bit ZIP and the compression.
Finally I decided to implement the ZIP archive format following wikipedia, this page. And I used extensively HxD tool on windows and 7z to test a lot.
At the end I found out that the format of the archive is simple, and I support only 1 disk, N files with directories, and compression based on zlib.
I also use zlib for crc32 checksum.
so if someone has the problem in the future, see the zip format on wikipedia and use zlib for crc32, then if you want compress your chunks with zlib.
Thanks to all of you

Good free lib for compression

I need compress library for following needs:
1) Packing directory in one file with extension of my choice ( .pack for example )
2) Work with content in this file (directory) without unpaking it
3) Mechanism for encrypting
I already know about zlib, but for me it is ugly documented and not written anywhere what features are support from it
Packing directory in one file with extension of my choice ( .pack for example )
Most archivers don't require you to use a particular file extension. Regardless, one can still invoke file on a file to guess its content type.
Work with content in this file (directory) without unpaking it
It's possible to create a file list manually and prepend any archive file with that. Often, .sh installers for Linux contain a shell script in the beginning, followed by some magic line like __ARCHIVE_START__ followed by a binary archive. Hence it is possible to read lines from a file until __ARCHIVE_START__ line has been read. The rest of the file is an archive file.
Mechanism for encrypting
One easy way is to use different libraries for archiving and encrypting:
Bundle the files into one .tar.
Archive .tar into say .tar.xz.
Prepend the .tar.xz with file list followed by __ARCHIVE_START__ line.
Encrypt the file with any encryption library you please.
What you want is not a compression library. You want a compression, archiving, and encryption library or libraries. You need archiving to put a directory of files into a single file.
You can use zlib to do the compress part, but not archive or encrypt. zlib is documented in zlib.h and you can see a usage example there, as well as many examples in the source distribution package.
You can construct your own archiving format, or you can use existing ones for which there are libraries such as zip or tar, both of which use or can be directed to use zlib.
You can use OpenSSL for strong encryption.