ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing a c program which will zip the files in the directories. It will read the directory names that have to be zipped from an xml file. For this i m using libxml parser. What i want to do is that "How to zip the files". Is there any libraries/Header files available under linux so that i can call the appropriate functions to zip the files. I want to do this with out using system function. Any one have ideas about this.
Originally posted by rajesh_b
[B
..
Is there any libraries/Header files available under linux so that i can call the appropriate functions to zip the files. I want to do this with out using system function. Any one have ideas about this.
Thankx in advance,
rajesh. [/B]
Hello, you can use the zlib library for this purpose. Here is an example code.. this is used for gzipping log files if they're oversized:
Code:
int ns_compress_file(char *file_path)
{
FILE *in_file; /* file that will be compressed */
gzFile gz_file; /* gzip file descriptor */
char *gz_file_path; /* file name that will be created */
char *in_file_buffer; /* buffer that will hold input file */
unsigned long in_file_size; /* input file's size in byte(s) */
if ( !(in_file = fopen(file_path, "r")) ) {
ns_log_event("Input file cannot be opened for compressing", LOG_ERR);
return -1;
}
/* add the extension's (.gz) length */
gz_file_path = (char *)malloc(strlen(file_path) + 4);
sprintf(gz_file_path, "%s.gz", file_path);
printf("DEBUG: output file will be %s\n", gz_file_path);
if ( !(gz_file = gzopen(gz_file_path, "wb")) ) {
ns_log_event("gzip file cannot be opened for writing", LOG_ERR);
fclose(in_file);
free(gz_file_path);
return -1;
}
in_file_size = ns_get_file_size_path(file_path);
in_file_buffer = (char *)malloc(in_file_size * BYTE);
if ( (fread(in_file_buffer, BYTE, in_file_size, in_file)) < in_file_size ) {
ns_log_event("Read error in input file at ns_compress_file()", LOG_ERR);
goto fire_exit;
}
/* write out the compressed file */
if ( !(gzwrite(gz_file, in_file_buffer, in_file_size)) ) {
ns_log_event("Error writing compressed file at ns_compressed_file()", \
LOG_ERR);
goto fire_exit;
} else { /* everything must be OK here */
free(gz_file_path);
free(in_file_buffer);
gzclose(gz_file);
/*
* remove the source file after creating compressed one
*/
unlink(file_path);
fclose(in_file);
return 0;
}
/*
* Here is an advantage of goto statement:
* if you don't like goto statement, write this
* cleanup code 3 or 4 times after each if statement.
* Lost of readability? No!
* i love goto :-)
*/
fire_exit:
free(gz_file_path);
free(in_file_buffer);
gzclose(gz_file);
fclose(in_file);
return -1;
}
Hai barisdemiray,
Thanx for u reply. I tried the code given by u. But i m getting errors when i am trying to compile it. the errrors are
/tmp/ccSDyhwf.o(.text+0x2d): In function `ns_compress_file':
: undefined reference to `ns_log_event'
/tmp/ccSDyhwf.o(.text+0x92): In function `ns_compress_file':
: undefined reference to `gzopen'
/tmp/ccSDyhwf.o(.text+0xad): In function `ns_compress_file':
: undefined reference to `ns_log_event'
/tmp/ccSDyhwf.o(.text+0xe3): In function `ns_compress_file':
: undefined reference to `ns_get_file_size_path'
/tmp/ccSDyhwf.o(.text+0x124): In function `ns_compress_file':
: undefined reference to `ns_log_event'
similarly there are other errors . But i have zlib.h file in "/usr/include" directory. I dont know why i am getting these errors.
Please help me.
As i _don't_ said, this code fragment is part of my current project named NetSentinel and the functions beginning with ns_* is implemented in my source tree. So it's normal to getting these errors. I will also copy the ns_get_file_size_path() function. ns_log_event calls could be replaced with perror("Aiiii! Some error occured") calls and then link the zlib library (like gcc -o code -lz code.c). Finally, there should be no problem anymore.
Here is the ns_get_file_size_path() function:
Code:
/*
* Returns the file size in bytes
* @param file_path Full path of the file
*/
unsigned long ns_get_file_size_path(char *file_path)
{
FILE *fp;
unsigned long file_size = 0L;
if ( !(fp = fopen(file_path, "r")) ) {
ns_log_event("Cannot open file", LOG_ERR);
return 0;
}
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
fclose(fp);
return file_size;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.