libtar 和 libz 的使用
用c代码生成 tar.gz 文件 实现的功能和 tar -zcf 命令一样
大概流程为
1、先用libtar相关函数对某个目录生成tar文件
2、然后对tar文件进行压缩
//tarFile -- like /home/user/test.tar
int tb_comparss_dir(char* srcDir, char* tarFile)
{
TAR *pTar;
char extractTo[] = ".";
char desFile[FILE_PATH_LEN] = { 0 };
char szBuf[10000] = { 0 };
ssize_t read_len;
FILE* tarFd;
gzFile gzFd;
memset(desFile, 0, sizeof(desFile));
sprintf(desFile, "%s.gz", tarFile);
if (tar_open(&pTar, tarFile, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) < 0)
{
xlog_error(__FILE__, __LINE__, "tar_open[%s] error[%s]", tarFile, strerror(errno));
return -1;
}
if (tar_append_tree(pTar, srcDir, extractTo) < 0)
{
close(tar_fd(pTar));
xlog_error(__FILE__, __LINE__, "tar_append_tree error[%s]", strerror(errno));
return -1;
}
close(tar_fd(pTar));
tarFd = fopen(tarFile, "rb");
if (tarFd == NULL) {
xlog_error(__FILE__, __LINE__, "fopen[%s] error[%s]", tarFile, strerror(errno));
return -1;
}
gzFd = gzopen(desFile, "wb");
if (gzFd == NULL) {
fclose(tarFd);
remove(tarFile);
fprintf(stderr, "gzopen error\n");
xlog_error(__FILE__, __LINE__, "gzopen[%s] error[%s]", tarFile, strerror(errno));
return -1;
}
while ((read_len = fread(szBuf, 1, 10000, tarFd)) > 0)
{
gzwrite(gzFd, szBuf, read_len);
}
gzclose(gzFd);
fclose(tarFd);
remove(tarFile);
return 0;
}
以上代码进行压缩某个目录,参数srcDir为要打包压缩的某个目录,tarFile 为要打包的文件名(.tar 结尾) 生成的文件最后未.tar.gz
解压缩和压缩过程相反,先解压,再解包
//uncomparss to srcDir
int tb_uncomparss_dir(char* srcDir, char* tarFile)
{
gzFile gzFd;
char szTmpFile[FILE_PATH_LEN] = { 0 };
FILE *fp;
int nReadLen = 0;
char szBuf[10000] = { 0 };
TAR* pTar;
if (strstr(tarFile, "tar.gz") == NULL)
{
xlog_error(__FILE__, __LINE__, "file[%s] is not end with .tar.gz", tarFile);
return -1;
}
if (access(tarFile, F_OK) < 0)
{
xlog_error(__FILE__, __LINE__, "not find file[%s]", tarFile);
return -1;
}
gzFd = gzopen(tarFile, "rb");
if (gzFd == NULL)
{
xlog_error(__FILE__, __LINE__, "gzopen file[%s] err[%s]", tarFile, strerror(errno));
return -1;
}
memset(szTmpFile, 0, sizeof(szTmpFile));
memcpy(szTmpFile, tarFile, strlen(tarFile) - 3); //remove .gz
fp = fopen(szTmpFile, "wb");
if (fp == NULL)
{
gzclose(gzFd);
xlog_error(__FILE__, __LINE__, "open file[%s] err[%s]", szTmpFile, strerror(errno));
return -1;
}
while ((nReadLen = gzread(gzFd, szBuf, 10000)) > 0)
{
fwrite(szBuf, nReadLen, 1, fp);
}
gzclose(gzFd);
fclose(fp);
if (tar_open(&pTar, szTmpFile, NULL, O_RDONLY, 0644, TAR_GNU) < 0)
{
unlink(szTmpFile);
xlog_error(__FILE__, __LINE__, "tar_open[%s] error[%s]", szTmpFile, strerror(errno));
return -1;
}
if (tar_extract_all(pTar, srcDir) < 0)
{
tar_close(pTar);
unlink(szTmpFile);
xlog_error(__FILE__, __LINE__, "tar_extract_all error[%s]", strerror(errno));
return -1;
}
tar_close(pTar);
unlink(szTmpFile);
return 0;
}
请主动忽略 xlog_error()函数
其中makefile 中需要添加动态库 -ltar -lz
libtar.so 可以用yum安装 libtar-devel 这个
- Ubuntu12.04安装64位系统出现编译错误error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or dir
问题: Ubuntu12.04安装64位系统出现编译错误error while loading shared libraries: libz.so.1: cannot open shared obje ...
- libz.so库分析
from:http://blog.chinaunix.net/uid-12773189-id-84605.html 1.查看库文件是由哪个软件包提供的空闲时打开/usr/lib目录(因为我知道这个目录 ...
- 编译Uboot时提示error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
在Ubuntu14.04 64位系统中已经安装了libc6:i386的库,编译Uboot时提示error while loading shared libraries: libz.so.1: cann ...
- libz.dylib
1. .dylib意味着这是一个动态链接库. 2. libz.dylib是提供zip压缩解压缩的库
- 使用LibZ合并.Net程序集,支持WPF
最近写了一个小的WPF程序,发布的时候发现依赖着两三个20~30k的小dll的,感觉有点不爽,就想把它合并一下.以前在WinForm下用过微软的ILMerge合并程序集,不过记得它对WPF程序支持不大 ...
- ios 9.1以后 添加libz.dylib 方法
1. 进入你项目的build phases 2.点击+号在弹出的对话框选择addother 3.在弹出的对话框中输入"cmd"+"shift"+"g& ...
- iOS开发libz.dylib介绍
libz.dylib这个Xcode系统库文件经常用到.这个其实是个动态链接库. 后缀名为.dylib的文件是一个动态库,这个库是运行时加载而不是编译时加载.这个也说明了obj-C是运行时语言,也就是数 ...
- xcode 编译错误找不到 libz.dylib
图片对应的是libxml2.dylib (libz.dylib 遇到的编译错误跟这个类似) 解决方法是在引入库的地方调整原先 比如libz.dylib 的目录: ================== ...
- /usr/local/lib/libz.a: could not read symbols: Bad value(64 位 Linux)
/usr/local/lib/libz.a: could not read symbols: Bad value(64 位 Linux) /usr/bin/ld: /usr/local/lib/lib ...
随机推荐
- .NET clickonce修改发布名称等
见图
- linux下用rpm 安装jdk
转载自http://blog.csdn.net/ldl22847/article/details/7605650 1.下载jdk的rpm安装包,这里以jdk-7u4-linux-i586.rpm为例进 ...
- selenium+PhantomJS简单爬虫
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2017年10月19日 @author: zzy ''' import tim ...
- 通用动态树(Link-Cut Tree)模板
一个没有维护任何东西的动态树模板 忘了怎么写可以直接来粘 int ch[300010][2], fa[300010], st[300010]; bool lazy[300010]; bool nroo ...
- 将0移到最后,在原数组操作,并且不能改变源数据顺序(JS编程)
一.问题描述: 将0移到最后,在原数组操作,并且不能改变源数据顺序. 示例:输入:[2,0,0,1,0,3], 结果:[2,1,3,0,0,0] 二.问题分析与解决: 注意是在原数组上操作,不要进行 ...
- UISearchBar 自定义处理
首先通过 KVC 获取到内部的 textField, 然后自定制处理 UITextField *searchField = [searchBar valueForKey:@"searchFi ...
- CentOS 6.* 安装node
CentOS6 安装node 使用node -v 是报错 ./node: error while loading shared libraries: libstdc++.so.6: cannot op ...
- Octave安装符号工具箱
1.国内访问Octave的代码包不稳定,可以访问网址http://sourceforge.mirrorservice.org/o/oc/octave/Octave%20Forge%20Packages ...
- 【转】Cannot add or update a child row: a foreign key constraint fails 解决办法
原因:设置的外键和对应的另一个表的主键值不匹配.解决方法:找出不匹配的值修改.或者清空两表数据. 转自https://blog.csdn.net/qq_29405421/article/details ...
- python学习,day2:列表的复制
主要涉及列表的潜复制(第二层受后面修改的影响)和深复制(不受后面修改的影响) 代码如下 # coding=utf-8 # Author: RyAn Bi import copy names = ['A ...