ZipFile.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. #include "../Config.h"
  8. #include "../Log/log.h"
  9. #include "../Define/define.h"
  10. #include "../ShareMemory/shmMem.h"
  11. //------------------------------------------------------------------------------
  12. void zipLogFiles(void)
  13. {
  14. const char *logPath = "/Storage/SystemLog";
  15. // 獲取目錄
  16. DIR *pDir = opendir(logPath);
  17. if (pDir != NULL) {
  18. struct timeb csuTime;
  19. struct tm *tmCSU;
  20. ftime(&csuTime);
  21. tmCSU = localtime(&csuTime.time);
  22. // log_info("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  23. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  24. // tmCSU->tm_sec);
  25. // Read items inside the folder
  26. struct dirent *pEntry = NULL;
  27. while ((pEntry = readdir(pDir)) != NULL) {
  28. if (strcmp(pEntry->d_name, ".") != 0 &&
  29. strcmp(pEntry->d_name, "..") != 0 &&
  30. strncmp(pEntry->d_name, "[", 1) == 0 &&
  31. strstr(pEntry->d_name, "tar") < 0) {
  32. char yearC[5];
  33. unsigned short year = 0;
  34. char monthC[3];
  35. unsigned short month = 0;
  36. yearC[4] = '\0';
  37. strncpy(yearC, pEntry->d_name + 1, 4);
  38. monthC[2] = '\0';
  39. strncpy(monthC, pEntry->d_name + 6, 2);
  40. year = atoi(yearC);
  41. month = atoi(monthC);
  42. if (year != 0) {
  43. if (year < tmCSU->tm_year + 1900 ||
  44. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1)) {
  45. log_info("tar file name : %s \n", pEntry->d_name);
  46. char file[256];
  47. memset(file, 0x00, sizeof(file));
  48. strcat(file, "tar zcvf ");
  49. strcat(file, logPath);
  50. strncat(file, "/", 1);
  51. strcat(file, pEntry->d_name);
  52. strcat(file, ".tar");
  53. strncat(file, " ", 1);
  54. strcat(file, logPath);
  55. strncat(file, "/", 1);
  56. strcat(file, pEntry->d_name);
  57. log_info("zip = %s \n", file);
  58. system(file);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. // Close folder
  65. closedir(pDir);
  66. }