Skip to content

Commit

Permalink
Add support for tar.xz and tar.lzma
Browse files Browse the repository at this point in the history
fixes #281
  • Loading branch information
larsgrefer committed Apr 5, 2022
1 parent d172ce5 commit 5fc2440
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public interface CompressFileOperations {

FileTree dumpTree(Object dumpFile);
FileTree dumpTree(Object dumpFile, String encoding);

FileTree tarXzTree(Object tarXzFile);
FileTree tarLzmaTree(Object tarXzFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.freefair.gradle.plugins.compress.internal;

import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.gradle.api.internal.file.archive.compression.CompressedReadableResource;
import org.gradle.api.internal.file.archive.compression.URIBuilder;
import org.gradle.api.resources.ResourceException;
import org.gradle.internal.resource.ResourceExceptions;

import java.io.*;
import java.net.URI;

/**
* @author Lars Grefer
* @see org.gradle.api.internal.file.archive.compression.AbstractArchiver
*/
public abstract class CommonsCompressArchiver implements CompressedReadableResource {

private final File file;
private final URI uri;

protected CommonsCompressArchiver(File file) {
assert file != null;

this.file = file;
this.uri = new URIBuilder(file.toURI()).schemePrefix(this.getSchemePrefix()).build();
}

@Override
public File getBackingFile() {
return file;
}

@Override
public CompressorInputStream read() throws ResourceException {
InputStream baseFile;
try {
baseFile = new BufferedInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
throw ResourceExceptions.readMissing(file, e);
}

try {
return read(baseFile);
} catch (IOException e) {
IOUtils.closeQuietly(baseFile);
throw ResourceExceptions.readFailed(file, e);
}
}

protected abstract CompressorInputStream read(InputStream in) throws IOException;

@Override
public String getBaseName() {
return file.getName();
}

@Override
public String getDisplayName() {
return file.getPath();
}

@Override
public URI getURI() {
return uri;
}

protected abstract String getSchemePrefix();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
import org.apache.commons.compress.archivers.dump.DumpArchiveInputStream;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.gradle.api.file.FileTree;
import org.gradle.api.internal.file.FileOperations;
import org.gradle.api.internal.file.collections.DirectoryFileTreeFactory;
import org.gradle.api.internal.file.collections.FileTreeAdapter;
import org.gradle.api.internal.file.temp.TemporaryFileProvider;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.resources.ReadableResource;
import org.gradle.api.tasks.util.PatternSet;
import org.gradle.internal.Factory;
import org.gradle.internal.hash.FileHasher;
Expand Down Expand Up @@ -126,6 +128,16 @@ private FileTree dumpTree(Object dumpFile, ArchiveInputStreamProvider<DumpArchiv
return new FileTreeAdapter(dumpFileTree, patternSetFactory);
}

public FileTree tarXzTree(Object tarXzFile) {
File file = fileOperations.file(tarXzFile);
return fileOperations.tarTree(new XzArchiver(file));
}

public FileTree tarLzmaTree(Object tarLzmaFile) {
File file = fileOperations.file(tarLzmaFile);
return fileOperations.tarTree(new LzmaArchiver(file));
}

private File getExpandDir() {
return temporaryFileProvider.newTemporaryFile("expandedArchives");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.freefair.gradle.plugins.compress.internal;

import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Lars Grefer
* @see org.gradle.api.internal.file.archive.compression.GzipArchiver
*/
public class LzmaArchiver extends CommonsCompressArchiver {

public LzmaArchiver(File xzFile) {
super(xzFile);
}

@Override
protected LZMACompressorInputStream read(InputStream in) throws IOException {
return new LZMACompressorInputStream(in);
}

@Override
protected String getSchemePrefix() {
return "lzma:";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.freefair.gradle.plugins.compress.internal;

import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Lars Grefer
* @see org.gradle.api.internal.file.archive.compression.GzipArchiver
*/
public class XzArchiver extends CommonsCompressArchiver {

public XzArchiver(File xzFile) {
super(xzFile);
}

@Override
protected XZCompressorInputStream read(InputStream in) throws IOException {
return new XZCompressorInputStream(in, true);
}

@Override
protected String getSchemePrefix() {
return "xz:";
}
}
5 changes: 4 additions & 1 deletion src/docs/asciidoc/_compress.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ FileTree sevenZipTree(Object sevenZipFile, char[] password);
FileTree dumpTree(Object dumpFile);
FileTree dumpTree(Object dumpFile, String encoding);
FileTree tarXzTree(Object tarXzFile);
FileTree tarLzmaTree(Object tarLzmaFile);
----

These methods can be used to open `ar`, `arj`, `cpio`, `7z` or `dump` archives.
These methods can be used to open `ar`, `arj`, `cpio`, `7z`, `dump`, `tar.xz` or `tar.lzma` archives.
They work the same way as the
https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:zipTree(java.lang.Object)[`zipTree`]
and
Expand Down

0 comments on commit 5fc2440

Please sign in to comment.