libtar 使用范例

首先要下载安装一下libtar,网上很容易找到,安装之后读读libtar.h这个文件。熟悉一下具体的function,function的说明

http://linux.die.net/man/3/tar_extract_all

下面是一个测试例子,看看就知道怎么用了。

      1 #include <tar.h>
      2 #include <libtar.h>
      3 #include <iostream>                                                           
      4 #include <fcntl.h>  
      5 using namespace std;                             
      6                                 
-     7 int main(){                                                      
|     8     // tar                       
|-    9     if(0){                                                        
||   10         TAR* tar_handle;                                                                                          
||   11         char* tar_fname = "test.tar";                                                     
||   12         tar_open(&tar_handle, tar_fname, NULL,  O_WRONLY | O_CREAT,  0644,  TAR_GNU);                             
||   13         char* subfile1 = "1.txt";                                                                                 
||   14         tar_append_file(tar_handle, subfile1,  subfile1);                                                         
||   15         char* subfile2 = "2.txt";                                                                                 
||   16         tar_append_file(tar_handle, subfile2,  subfile2);                                                         
||   17         tar_close(tar_handle);                                                                                    
||   18     }                                                                                                             
|    19                                                                                                                   
|    20     // untar                                                                                                      
|-   21     if(1){                                                                                                        
||   22         TAR* tar_handle;                                                                                          
||   23         char* tar_fname = "test.tar";                                                     
||   24         tar_open(&tar_handle, tar_fname, NULL,  O_RDONLY,  0644,  TAR_GNU);                                       
||   25         char* savefold = "temp";                                                                                  
||   26         tar_extract_all(tar_handle, "temp");                                                                      
||   27         tar_close(tar_handle);                                                                                    
||   28     }                                                                                                             
|    29                                                                                                                   
|    30     return 0;                                                                                                     
|    31 }  


有时候添加文件有问题

-    56 inline int tarAppendFile(const string& tar_fname, const string& add_fname, const string& in_tar_name){
|    57     off_t tarFileEnd;  // keeps read position it tarball
|    58     char* tarFileName = const_cast<char*>(tar_fname.c_str());
|    59     TAR* tar;
|-   60     if (tar_open(&tar,  tarFileName,  NULL,  O_RDONLY,  0,  0)  ==  -1) {
||   61         return -1;
||   62     }
|    63     // go through all files in tarball and record end position of the  last file read
|-   64     while (th_read(tar)  ==  0) {
||-  65         if (TH_ISREG(tar)) {
|||  66             tar_skip_regfile(tar);
|||  67         }
||   68         tarFileEnd = lseek(tar->fd,  0,  SEEK_CUR);
||   69     }
|    70     // at this point,  tarFileEnd is position where EOT block begins
|    71     tar_close(tar);  // close for reading
|    72     //truncate EOT from the tarball
|-   73     if (truncate(tarFileName,  tarFileEnd)  ==  -1) {
||   74         return -1;
||   75     }
|    76     // open truncated tarball (without EOT block) for writing and append
|    77     if (tar_open(&tar,  tarFileName,  NULL,  O_WRONLY | O_APPEND,  0666,  0)
|-   78             ==  -1) {
||   79         return -1;
||   80     }
|    81     // add files
|    82     //while (... more files to add...) {
|-   83     if (tar_append_file(tar,  const_cast<char*>(add_fname.c_str()),  const_cast<char*>(in_tar_name.c_str()))  ==  -1) {
||   84         tar_close(tar);
||   85         return -1;
||   86     }
|    87     // add EOT at the end and close tarball
|    88     tar_append_eof(tar);
|    89     tar_close(tar);
|    90     return 0;
|    91 }



参照资料:

Well, it isn't that straightforward to add new files to an existing  
tarball with libtar. First of all, since you do:

tar_open(&tar, tarFileName, NULL, O_WRONLY, 0666, TAR_VERBOSE);

you've opened new file for writing and truncated it to zero at the  
very beginning. You should do something like:

tar_open(&tar, tarFileName, NULL, O_WRDONLY | O_APPEND, 0666, 0);
tar_append_file(tar, myFileName, inTarFileName);
tar_append_eof(tar);
tar_close(tar);

This will work, but only partially. The existing tarball probably has  
EOT (end of tar) block (20 bytes of all zeros). WIth the code above,  
you would add new files but after already existing EOT block. Some  
tar utilities have an option to ignore  EOT blocks (with CLI tar its  
option -i), so with those utilities you'd be able to see new files in  
the tarball. But most utilities don't support that option, so newly  
added files will remain invisible to them! You can make starting  
tarball much bigger by adding a lot of files to it, but those files  
won't be seen by vast majority of the tar utilities. Even if you try  
to read modified tarball with libtar, you won't be able to see newly  
added files, unless you specify TAR_IGNORE_EOT option. You need to  
remove existing EOT before adding new files.

So, this is the way that I managed to regularly add files to existing  
tarball using libtar:

off_t tarFileEnd; // keeps read position it tarball
// open for reading first to determine where EOT block begins		 	
if (tar_open(&tar, tarFileName, NULL, O_RDONLY, 0, 0) == -1) {
	return -1;
}
// go through all files in tarball and record end position of the  
last file read
while (th_read(tar) == 0) {
	if (TH_ISREG(tar)) {
		tar_skip_regfile(tar);
	}
	tarFileEnd = lseek(tar->fd, 0, SEEK_CUR);
}
// at this point, tarFileEnd is position where EOT block begins
tar_close(tar); // close for reading
//truncate EOT from the tarball
if (truncate(tarFileName, tafFileEnd) == -1) {
	return -1;
}
// open truncated tarball (without EOT block) for writing and append
if (tar_open(&tar, tarFileName, NULL, O_WRDONLY | O_APPEND, 0666, 0)  
== -1) {
	return -1;
}
// add files
while (... more files to add...) {
	if (tar_append_file(tar, myFileName, inTarFileName) == -1) {
		tar_close(tar);
		return -1;
}
// add EOT at the end and close tarball
tar_append_eof(tar);
tar_close(tar);
return;

Try this approach, it should work.

Milke


你可能感兴趣的:(libtar 使用范例)