001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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        boolean compressionSucceeded = false;
066        try (FileInputStream fis = new FileInputStream(originalFileName);
067             ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compressedFileName), BUFFER_SIZE))) {
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            compressionSucceeded = true;
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
085        // Delete the original only after successful compression so a failure leaves it intact.
086        if (compressionSucceeded) {
087            if (!file2zip.delete()) {
088                addStatus(new WarnStatus("Could not delete [" + originalFileName + "].", this));
089            }
090        } else {
091            addStatus(new WarnStatus("Compression of [" + originalFileName + "] failed. Original file left intact.", this));
092        }
093    }
094
095    // http://jira.qos.ch/browse/LBCORE-98
096    // The name of the compressed file as nested within the zip archive
097    //
098    // Case 1: RawFile = null, Pattern = foo-%d.zip
099    // nestedFilename = foo-${current-date}
100    //
101    // Case 2: RawFile = hello.txt, Pattern = = foo-%d.zip
102    // nestedFilename = foo-${current-date}
103    //
104    // in both cases, the strategy consisting of removing the compression
105    // suffix of zip file works reasonably well. The alternative strategy
106    // whereby the nested file name was based on the value of the raw file name
107    // (applicable to case 2 only) has the disadvantage of the nested files
108    // all having the same name, which could make it harder for the user
109    // to unzip the file without collisions
110    //ZipEntry computeZipEntry(File zippedFile) {
111    //    return computeZipEntry(zippedFile.getName());
112    //}
113
114    ZipEntry computeZipEntry(String filename) {
115        String nameOfFileNestedWithinArchive = Compressor.computeFileNameStrWithoutCompSuffix(filename, CompressionMode.ZIP);
116        return new ZipEntry(nameOfFileNestedWithinArchive);
117    }
118}