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