Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#755 XZ compression support added #823

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions logback-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
Expand Down Expand Up @@ -180,6 +186,7 @@
org.fusesource.jansi;resolution:=optional,
org.codehaus.janino;resolution:=optional,
org.codehaus.commons.compiler;resolution:=optional,
org.tukaani.xz;resolution:=optional,
*
</Import-Package>
</instructions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void rollover() throws RolloverFailure {
util.rename(getActiveFileName(), fileNamePattern.convertInt(minIndex));
break;
case GZ:
case XZ:
compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex), null);
break;
case ZIP:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ protected void determineCompressionMode() {
} else if (fileNamePatternStr.endsWith(".zip")) {
addInfo("Will use zip compression");
compressionMode = CompressionMode.ZIP;
} else if (fileNamePatternStr.endsWith(".xz")) {
addInfo("Will use xz compression");
compressionMode = CompressionMode.XZ;
} else {
addInfo("No compression will be used");
compressionMode = CompressionMode.NONE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
package ch.qos.logback.core.rolling.helper;

public enum CompressionMode {
NONE, GZ, ZIP;
NONE, GZ, ZIP, XZ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream;

import ch.qos.logback.core.rolling.RolloverFailure;
import ch.qos.logback.core.spi.ContextAwareBase;
Expand All @@ -30,7 +32,7 @@
import ch.qos.logback.core.util.FileUtil;

/**
* The <code>Compression</code> class implements ZIP and GZ file
* The <code>Compression</code> class implements ZIP,GZ and XZ file
* compression/decompression methods.
*
* @author Ceki G&uuml;lc&uuml;
Expand All @@ -56,6 +58,9 @@ public void compress(String nameOfFile2Compress, String nameOfCompressedFile, St
case GZ:
gzCompress(nameOfFile2Compress, nameOfCompressedFile);
break;
case XZ:
xzCompress(nameOfFile2Compress, nameOfCompressedFile);
break;
case ZIP:
zipCompress(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
break;
Expand Down Expand Up @@ -189,6 +194,50 @@ private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {

}

private void xzCompress(String nameOfFile2xz, String nameOfxzedFile) {
File file2xz = new File(nameOfFile2xz);

if (!file2xz.exists()) {
addStatus(new WarnStatus("The file to compress named [" + nameOfFile2xz + "] does not exist.", this));

return;
}

if (!nameOfxzedFile.endsWith(".xz")) {
nameOfxzedFile = nameOfxzedFile + ".xz";
}

File xzedFile = new File(nameOfxzedFile);

if (xzedFile.exists()) {
addWarn("The target compressed file named [" + nameOfxzedFile
+ "] exist already. Aborting file compression.");
return;
}

addInfo("XZ compressing [" + file2xz + "] as [" + xzedFile + "]");
createMissingTargetDirsIfNecessary(xzedFile);

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(nameOfFile2xz));
XZOutputStream xzos = new XZOutputStream(new FileOutputStream(nameOfxzedFile), new LZMA2Options())) {

byte[] inbuf = new byte[BUFFER_SIZE];
int n;

while ((n = bis.read(inbuf)) != -1) {
xzos.write(inbuf, 0, n);
}
} catch (Exception e) {
addStatus(new ErrorStatus(
"Error occurred while compressing [" + nameOfFile2xz + "] into [" + nameOfxzedFile + "].", this,
e));
}

if (!file2xz.delete()) {
addStatus(new WarnStatus("Could not delete [" + nameOfFile2xz + "].", this));
}
}

static public String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr,
CompressionMode compressionMode) {
int len = fileNamePatternStr.length();
Expand All @@ -198,6 +247,11 @@ static public String computeFileNameStrWithoutCompSuffix(String fileNamePatternS
return fileNamePatternStr.substring(0, len - 3);
else
return fileNamePatternStr;
case XZ:
if (fileNamePatternStr.endsWith(".xz"))
return fileNamePatternStr.substring(0, len - 3);
else
return fileNamePatternStr;
case ZIP:
if (fileNamePatternStr.endsWith(".zip"))
return fileNamePatternStr.substring(0, len - 4);
Expand Down
3 changes: 3 additions & 0 deletions logback-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// optionally require jansi
requires static org.fusesource.jansi;

// optionally require tukaani
requires static org.tukaani.xz;

exports ch.qos.logback.core;
exports ch.qos.logback.core.boolex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@ public void setUp() throws IOException {
File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt");

copy(source, dest);
File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
target.mkdirs();
target.delete();
File gzTarget = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
gzTarget.mkdirs();
gzTarget.delete();
File xzTarget = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.xz");
xzTarget.mkdirs();
xzTarget.delete();
}
{
File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.copy");
File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt");
copy(source, dest);
File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz");
target.mkdirs();
target.delete();
File gzTarget = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz");
gzTarget.mkdirs();
gzTarget.delete();
File xzTarget = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.xz");
xzTarget.mkdirs();
xzTarget.delete();
}
{
File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.copy");
Expand All @@ -69,7 +75,7 @@ public void setUp() throws IOException {
}

@Test
public void test1() throws Exception {
public void test_gz_1() throws Exception {
Compressor compressor = new Compressor(CompressionMode.GZ);
compressor.setContext(context);
compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt",
Expand All @@ -82,7 +88,7 @@ public void test1() throws Exception {
}

@Test
public void test2() throws Exception {
public void test_gz_2() throws Exception {
Compressor compressor = new Compressor(CompressionMode.GZ);
compressor.setContext(context);
compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt",
Expand All @@ -96,7 +102,7 @@ public void test2() throws Exception {
}

@Test
public void test3() throws Exception {
public void test_zip_1() {
Compressor compressor = new Compressor(CompressionMode.ZIP);
compressor.setContext(context);
compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.txt",
Expand All @@ -110,6 +116,32 @@ public void test3() throws Exception {
// + "witness/compress3.txt.zip"));
}

@Test
public void test_xz_1() throws Exception {
Compressor compressor = new Compressor(CompressionMode.XZ);
compressor.setContext(context);
compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt",
CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.xz", null);

StatusChecker checker = new StatusChecker(context);
Assertions.assertTrue(checker.isErrorFree(0));
Assertions.assertTrue(Compare.xzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.xz",
CoreTestConstants.TEST_SRC_PREFIX + "witness/compress1.txt.xz"));
}

@Test
public void test_xz_2() throws Exception {
Compressor compressor = new Compressor(CompressionMode.XZ);
compressor.setContext(context);
compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt",
CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.xz", null);

StatusChecker checker = new StatusChecker(context);
Assertions.assertTrue(checker.isErrorFree(0));
Assertions.assertTrue(Compare.xzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.xz",
CoreTestConstants.TEST_SRC_PREFIX + "witness/compress2.txt.xz"));
}

private void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);) {
Expand Down
50 changes: 41 additions & 9 deletions logback-core/src/test/java/ch/qos/logback/core/util/Compare.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package ch.qos.logback.core.util;

import org.tukaani.xz.XZInputStream;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -138,12 +140,45 @@ private static void outputFile(String file) throws FileNotFoundException, IOExce
}
}

public static boolean gzCompare(String file1, String file2) throws FileNotFoundException, IOException {
BufferedReader in1 = null;
BufferedReader in2 = null;
try {
in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file1))));
in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file2))));
public static boolean gzCompare(String file1, String file2) throws IOException {
try (BufferedReader in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file1))));
BufferedReader in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file2))));) {

String s1;
int lineCounter = 0;

while ((s1 = in1.readLine()) != null) {
lineCounter++;

String s2 = in2.readLine();

if (!s1.equals(s2)) {
System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter);
System.out.println("One reads: [" + s1 + "].");
System.out.println("Other reads:[" + s2 + "].");
outputFile(file1);
outputFile(file2);

return false;
}
}

// the second file is longer
if (in2.read() != -1) {
System.out.println("File [" + file2 + "] longer than file [" + file1 + "].");
outputFile(file1);
outputFile(file2);

return false;
}

return true;
}
}

public static boolean xzCompare(String file1, String file2) throws IOException {
try (BufferedReader in1 = new BufferedReader(new InputStreamReader(new XZInputStream(new FileInputStream(file1))));
BufferedReader in2 = new BufferedReader(new InputStreamReader(new XZInputStream(new FileInputStream(file2))))) {

String s1;
int lineCounter = 0;
Expand Down Expand Up @@ -174,9 +209,6 @@ public static boolean gzCompare(String file1, String file2) throws FileNotFoundE
}

return true;
} finally {
close(in1);
close(in2);
}
}

Expand Down
Binary file added logback-core/src/test/witness/compress1.txt.xz
Binary file not shown.
Binary file added logback-core/src/test/witness/compress2.txt.xz
Binary file not shown.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<!--<jansi.version>1.18</jansi.version>-->
<jansi.version>2.4.0</jansi.version>

<tukaani.version>1.9</tukaani.version>

<mockito-core.version>4.8.0</mockito-core.version>
<byte-buddy.version>1.12.14</byte-buddy.version>

Expand Down Expand Up @@ -200,6 +202,12 @@
<version>${jansi.version}</version>
</dependency>

<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>${tukaani.version}</version>
</dependency>

<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
Expand Down