001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2025, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.rolling.helper;
016
017import ch.qos.logback.core.status.ErrorStatus;
018import ch.qos.logback.core.status.WarnStatus;
019
020import java.io.BufferedInputStream;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.FileOutputStream;
024import java.util.zip.ZipEntry;
025import java.util.zip.ZipOutputStream;
026
027/**
028 * Compresses files using JDK's Zip compression algorithm.
029 *
030 * @author Ceki Gülcü
031 * @since 1.5.18
032 */
033public class ZipCompressionStrategy extends CompressionStrategyBase {
034
035    @Override
036    public void compress(String originalFileName, String compressedFileName, String innerEntryName) {
037
038        File file2zip = new File(originalFileName);
039
040        if (!file2zip.exists()) {
041            addStatus(new WarnStatus("The file to compress named [" + originalFileName + "] does not exist.", this));
042
043            return;
044        }
045
046        if (innerEntryName == null) {
047            addStatus(new WarnStatus("The innerEntryName parameter cannot be null", this));
048            return;
049        }
050
051        if (!compressedFileName.endsWith(".zip")) {
052            compressedFileName = compressedFileName + ".zip";
053        }
054
055        File zippedFile = new File(compressedFileName);
056
057        if (zippedFile.exists()) {
058            addStatus(new WarnStatus("The target compressed file named [" + compressedFileName + "] exist already.", this));
059
060            return;
061        }
062
063        addInfo("ZIP compressing [" + file2zip + "] as [" + zippedFile + "]");
064        createMissingTargetDirsIfNecessary(zippedFile);
065
066        try (FileInputStream fis = new FileInputStream(originalFileName);
067             ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFileName))) {
068
069            ZipEntry zipEntry = computeZipEntry(innerEntryName);
070            zos.putNextEntry(zipEntry);
071
072            byte[] inbuf = new byte[BUFFER_SIZE];
073            int n;
074
075            while ((n = fis.read(inbuf)) != -1) {
076                zos.write(inbuf, 0, n);
077            }
078
079            addInfo("Done ZIP compressing [" + file2zip + "] as [" + zippedFile + "]");
080        } catch (Exception e) {
081            addStatus(new ErrorStatus("Error occurred while compressing [" + originalFileName + "] into [" + compressedFileName + "].", this, e));
082        }
083        if (!file2zip.delete()) {
084            addStatus(new WarnStatus("Could not delete [" + originalFileName + "].", this));
085        }
086    }
087
088    // http://jira.qos.ch/browse/LBCORE-98
089    // The name of the compressed file as nested within the zip archive
090    //
091    // Case 1: RawFile = null, Pattern = foo-%d.zip
092    // nestedFilename = foo-${current-date}
093    //
094    // Case 2: RawFile = hello.txt, Pattern = = foo-%d.zip
095    // nestedFilename = foo-${current-date}
096    //
097    // in both cases, the strategy consisting of removing the compression
098    // suffix of zip file works reasonably well. The alternative strategy
099    // whereby the nested file name was based on the value of the raw file name
100    // (applicable to case 2 only) has the disadvantage of the nested files
101    // all having the same name, which could make it harder for the user
102    // to unzip the file without collisions
103    //ZipEntry computeZipEntry(File zippedFile) {
104    //    return computeZipEntry(zippedFile.getName());
105    //}
106
107    ZipEntry computeZipEntry(String filename) {
108        String nameOfFileNestedWithinArchive = Compressor.computeFileNameStrWithoutCompSuffix(filename, CompressionMode.ZIP);
109        return new ZipEntry(nameOfFileNestedWithinArchive);
110    }
111}