ZipFile.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. void zipLogFiles(void)
  2. {
  3. const char *logPath = "/Storage/SystemLog";
  4. // 獲取目錄
  5. DIR *pDir = opendir(logPath);
  6. if (pDir != NULL) {
  7. struct timeb csuTime;
  8. struct tm *tmCSU;
  9. ftime(&csuTime);
  10. tmCSU = localtime(&csuTime.time);
  11. // log_info("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  12. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  13. // tmCSU->tm_sec);
  14. // Read items inside the folder
  15. struct dirent *pEntry = NULL;
  16. while ((pEntry = readdir(pDir)) != NULL) {
  17. if (strcmp(pEntry->d_name, ".") != 0 &&
  18. strcmp(pEntry->d_name, "..") != 0 &&
  19. strncmp(pEntry->d_name, "[", 1) == 0 &&
  20. strstr(pEntry->d_name, "tar") < 0) {
  21. char yearC[5];
  22. unsigned short year = 0;
  23. char monthC[3];
  24. unsigned short month = 0;
  25. yearC[4] = '\0';
  26. strncpy(yearC, pEntry->d_name + 1, 4);
  27. monthC[2] = '\0';
  28. strncpy(monthC, pEntry->d_name + 6, 2);
  29. year = atoi(yearC);
  30. month = atoi(monthC);
  31. if (year != 0) {
  32. if (year < tmCSU->tm_year + 1900 ||
  33. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1)) {
  34. log_info("tar file name : %s \n", pEntry->d_name);
  35. char file[256];
  36. memset(file, 0x00, sizeof(file));
  37. strcat(file, "tar zcvf ");
  38. strcat(file, logPath);
  39. strncat(file, "/", 1);
  40. strcat(file, pEntry->d_name);
  41. strcat(file, ".tar");
  42. strncat(file, " ", 1);
  43. strcat(file, logPath);
  44. strncat(file, "/", 1);
  45. strcat(file, pEntry->d_name);
  46. log_info("zip = %s \n", file);
  47. system(file);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. // Close folder
  54. closedir(pDir);
  55. }