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;
019import java.io.BufferedOutputStream;
020import java.io.File;
021import java.io.FileInputStream;
022import java.io.FileOutputStream;
023import java.util.zip.ZipEntry;
024import java.util.zip.ZipOutputStream;
025
026/**
027 * Compresses files using JDK's Zip compression algorithm.
028 *
029 * @author Ceki Gülcü
030 * @since 1.5.18
031 */
032public class ZipCompressionStrategy extends CompressionStrategyBase {
033
034    @Override
035    public void compress(String originalFileName, String compressedFileName, String innerEntryName) {
036
037        File file2zip = new File(originalFileName);
038
039        if (!file2zip.exists()) {
040            addStatus(new WarnStatus("The file to compress named [" + originalFileName + "] does not exist.", this));
041
042            return;
043        }
044
045        if (innerEntryName == null) {
046            addStatus(new WarnStatus("The innerEntryName parameter cannot be null", this));
047            return;
048        }
049
050        if (!compressedFileName.endsWith(".zip")) {
051            compressedFileName = compressedFileName + ".zip";
052        }
053
054        File zippedFile = new File(compressedFileName);
055
056        if (zippedFile.exists()) {
057            addStatus(new WarnStatus("The target compressed file named [" + compressedFileName + "] exist already.", this));
058
059            return;
060        }
061
062        addInfo("ZIP compressing [" + file2zip + "] as [" + zippedFile + "]");
063        createMissingTargetDirsIfNecessary(zippedFile);
064
065        try (FileInputStream fis = new FileInputStream(originalFileName);
066             ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compressedFileName), BUFFER_SIZE))) {
067
068            ZipEntry zipEntry = computeZipEntry(innerEntryName);
069            zos.putNextEntry(zipEntry);
070
071            byte[] inbuf = new byte[BUFFER_SIZE];
072            int n;
073
074            while ((n = fis.read(inbuf)) != -1) {
075                zos.write(inbuf, 0, n);
076            }
077
078            addInfo("Done ZIP compressing [" + file2zip + "] as [" + zippedFile + "]");
079        } catch (Exception e) {
080            addStatus(new ErrorStatus("Error occurred while compressing [" + originalFileName + "] into [" + compressedFileName + "].", this, e));
081        }
082        if (!file2zip.delete()) {
083            addStatus(new WarnStatus("Could not delete [" + originalFileName + "].", this));
084        }
085    }
086
087    // http://jira.qos.ch/browse/LBCORE-98
088    // The name of the compressed file as nested within the zip archive
089    //
090    // Case 1: RawFile = null, Pattern = foo-%d.zip
091    // nestedFilename = foo-${current-date}
092    //
093    // Case 2: RawFile = hello.txt, Pattern = = foo-%d.zip
094    // nestedFilename = foo-${current-date}
095    //
096    // in both cases, the strategy consisting of removing the compression
097    // suffix of zip file works reasonably well. The alternative strategy
098    // whereby the nested file name was based on the value of the raw file name
099    // (applicable to case 2 only) has the disadvantage of the nested files
100    // all having the same name, which could make it harder for the user
101    // to unzip the file without collisions
102    //ZipEntry computeZipEntry(File zippedFile) {
103    //    return computeZipEntry(zippedFile.getName());
104    //}
105
106    ZipEntry computeZipEntry(String filename) {
107        String nameOfFileNestedWithinArchive = Compressor.computeFileNameStrWithoutCompSuffix(filename, CompressionMode.ZIP);
108        return new ZipEntry(nameOfFileNestedWithinArchive);
109    }
110}