安卓中如何压缩日志文件以及文件如何快速生成

本文介绍了一种在安卓设备上定期压缩并上传日志文件的方法,以避免因日志积累导致的内存不足问题。通过读取外部存储的日志文件,按日期筛选并压缩超过一定时间的文件,确保设备正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近的安卓项目要添个日志,添上日志后又要将日志文件定期压缩上传,保证安卓设备中不会因日志文件过多导致内存不够(pos机器之类的内存不大)而导致程序运行出问题,于是要定期压缩日志文件上传后台,代码及注释如下:

 //读取安卓机器外部存储路径下的日志文件,隔天去压缩前一天的日志文件,原日志文件名形同“2019-02-03Log”
    public static void readFolder(){
        String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "logs";
        File file = new File(path);
        File [] files = file.listFiles();
        for(File fileChild:files){
            if(fileChild.isFile()){
                String fileName = fileChild.getName();
                Logger.i("fileName is %s",fileName);
                String[] strings = fileName.split("L");
                String dateName = strings[0].replace("-","");
                Date dateNow = new Date();
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
                String dateNowValue =sdf2.format(dateNow);
                int result = Integer.parseInt(dateNowValue)-Integer.parseInt(dateName);
                Logger.i("result is %s",result);
                Logger.i("path is %s",path);
                if(result>=0){
                    Logger.i("fileName is %s",fileName);
                    ZipFile(path+File.separator +fileName,path+File.separator +dateName+"Log"+".zip");
                }

            }
        }
    }
//创建zip文件
public static void ZipFile(String FilePath, String zipFilePath)  {
    try {
        ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFilePath));
        File file = new File(FilePath);
        Logger.i("file.getParent() is %s",file.getParent());
        Logger.i("file.getAbsolutePath() is %s",file.getAbsolutePath());
        ZipFiles(file.getParent() + File.separator, file.getName(), outZip);
        outZip.finish();
        outZip.close();
    }catch (Exception e){
        e.getStackTrace();
    }
}
//压缩文件到zip文件中
private static void ZipFiles(String filePath, String fileName, ZipOutputStream zipOutputSteam) throws Exception {
     Logger.i("folderString is %s" ,folderString + "\n" +
            "fileString is %s" , fileString + "\n==========================");
    if (zipOutputSteam == null)
        return;
    File file = new File(filePath+ fileName);
    if (file.isFile()) {
        ZipEntry zipEntry = new ZipEntry(fileString);
        FileInputStream inputStream = new FileInputStream(file);
        zipOutputSteam.putNextEntry(zipEntry);
        int len;
        byte[] buffer = new byte[4096];
        while ((len = inputStream.read(buffer)) != -1) {
            zipOutputSteam.write(buffer, 0, len);
        }
        zipOutputSteam.closeEntry();
    }}

因为规定一次压缩文件上传的大小不能太大,只能2M左右,于是我又去测试了一天的交易量产生的日志最多200多M,于是我试着生成一个300M的txt日志文件,测试压缩后的文件大小,压缩结果为2M左右,结果就符合了要求,这里提一下如何生成300M的文件:

一种是Windows自带的可以生成一个空文件的但是大小是可以指定的方法,但是这种方法产生的文件压缩并不等同与实在的文件压缩的大小,所以后面抛弃了这种方法;---win+R,输入”fsutil file creatnew test.txt 1024”产生1M文件,同理得想要大小文件。

第二种是采用的是.bat命令生成的,这种方法可以将一个文件循环写入另一个文件中,命令如下,0.txt是先自己生成的基础文件,可以输入一些数据保证一定大小,1.txt是要生成的目标文件,50是循环次数,这段代码输入在另一个文件中最后保存成.bat的形式运行就可以了。

@echo off      

for /l %%n in (0,1,50) do (   

type 0.txt >> 1.txt       

)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值