将某目录文件打成tar包

要点:

1、char tarName[100] = {0};
      sprintf(tarName , "tar -cvf data.tar %s" , tarPath);// 把某个路径(tarPath)做成一个名为 data.tar 的归档包

    //其中sprintf()的作用是将后面的路径做成一个字符串

2、system(tarName);

     //system(const  char*) 是系统调用执行该命令(命令已写成字符串形式)


源代码:

 #include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>

//这是初始包目录 做成一个初始包
char *tarPath = "/home/client/walk_dir/test/"; 
#define TAR_LARGE_SIZE 4*1024*1024


void send_small_file(char *path)
{
char tarName[100] = {0};
// 把某个路径(tarPath)做成一个名为 data.tar 的归档包
sprintf(tarName , "tar -cvf data.tar %s" , tarPath);
system(tarName);

struct stat stateTar;
if (stat(path, &stateTar) != 0) {
puts("file does not exist! ignored!\n");
return;
}

struct stat state;
if (stat(path, &state) != 0) {
puts("file does not exist! ignored!\n");
return;
}
//往包tar里面追加新的文件 ,小于4兆则添加(打包成小于4兆的文件)
if(stateTar.st_size + state.st_size  <= TAR_LARGE_SIZE)
{
  char tempCmd[100] = {0};
  sprintf(tempCmd , "tar -rv -f data.tar %s" , path);
  system(tempCmd);
  printf("tar tianjia cheng gong!\n");
  return ;
}
else 
{
printf("tar yi man!\n");//tar的路径可以使用了
return ;
}
}


int main(int arg , char** arv)
{
  char *path = "/home/client/walk_dir/temp/";
  send_small_file(path);
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值