Is it possible to store file in Zip(compressed log.txt file) with in cache directory
Closed this issue · 1 comments
lkhore commented
Is it possible to store the file in a Zip(compressed log.txt file) within the cache directory due to size constraint
alessandro-bottamedi commented
I would suggest using the date as the file name and zipping the file of the previous day (if it exist) upon app startup. I'll write you an example using the package react-native-zip-archive
:
import { zip } from 'react-native-zip-archive'
import RNFS from 'react-native-fs'
import { logger, fileAsyncTransport } from "react-native-logs"
let date = new Date();
date.setDate(date.getDate() - 1);
let d = date.getDate();
let m = date.getMonth() + 1;
let y = date.getFullYear();
const targetPath = `${DocumentDirectoryPath}/logs_${d}-${m}-${y}.zip`
const sourcePath = `${DocumentDirectoryPath}/logs_${d}-${m}-${y}`
RNFS.exists(sourcePath)
.then((exists) => {
if (exists) {
zip(sourcePath, targetPath)
.then((path) => {
RNFS.unlink(filePath)
})
.catch((error) => {
console.error(error)
})
})
.catch((error) => {
console.error(error);
});
const config = {
severity: "debug",
transport: fileAsyncTransport,
transportOptions: {
FS: RNFS,
fileName: `logs_{date-today}`, // Create a new file every day
},
};
I haven't tested this code; there might be errors...