LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2004, 05:28 AM   #1
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Rep: Reputation: 15
Question Zipping of files


Hi

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.

Thankx in advance,
rajesh.
 
Old 10-20-2004, 06:31 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I think you want zlib.
 
Old 10-20-2004, 06:34 AM   #3
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Re: Zipping of files

Quote:
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;
}
Don't forget to include zlib.h header.
 
Old 10-20-2004, 07:05 AM   #4
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Original Poster
Rep: Reputation: 15
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.
 
Old 10-20-2004, 11:02 AM   #5
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Ooops! Sorry about that.

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;
}
 
Old 10-27-2004, 06:02 AM   #6
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Original Poster
Rep: Reputation: 15
Thanx for u r help
It worked out.

Rajesh.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with zipping and unzipping Tinku Linux - General 2 11-11-2005 10:14 AM
Compressing/Zipping A Directory Wha?Where? Linux - Newbie 4 07-07-2005 09:20 PM
zipping files with a script quickk Linux - Newbie 8 09-17-2004 09:32 AM
zipping/compressing folders and files DiZASTiX Linux - Newbie 1 05-26-2003 08:48 PM
Zipping and Taring and ? Goatdemon Linux - Newbie 3 05-22-2002 07:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration