-
-
Save MelanX/6b3924bbcf7157f4a200d8309f89dffd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.melanx.simplebackups.compression; | |
import de.melanx.simplebackups.config.CommonConfig; | |
import org.apache.commons.compress.compressors.CompressorException; | |
import org.apache.commons.compress.compressors.CompressorOutputStream; | |
import org.apache.commons.compress.compressors.CompressorStreamFactory; | |
import org.tukaani.xz.LZMA2Options; | |
import org.tukaani.xz.XZOutputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.FileVisitResult; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.SimpleFileVisitor; | |
import java.nio.file.attribute.BasicFileAttributes; | |
public class XzCompression extends CompressionBase { | |
@Override | |
public long makeBackup(Path outputFile, Path levelName, Path levelPath, long lastSaved) throws IOException { | |
FileOutputStream outputStream = new FileOutputStream(outputFile.toFile()); | |
try { | |
CompressorOutputStream zstdOut = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.ZSTANDARD, outputStream); | |
Files.walkFileTree(levelPath.resolve(levelName), new SimpleFileVisitor<>() { | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
if (!file.endsWith("session.lock")) { | |
long lastModified = file.toFile().lastModified(); | |
if (!CommonConfig.onlyModified() || lastModified - lastSaved > 0) { | |
com.google.common.io.Files.asByteSource(file.toFile()).copyTo(zstdOut); | |
} | |
} | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} catch (CompressorException e) { | |
throw new RuntimeException(e); | |
} | |
return 0; | |
} | |
// public long makeBackup(Path outputFile, Path levelName, Path levelPath, long lastSaved) throws IOException { | |
// FileOutputStream outputStream = new FileOutputStream(outputFile.toFile()); | |
// XZOutputStream xzOut = new XZOutputStream(outputStream, new LZMA2Options()); | |
// | |
// Files.walkFileTree(levelPath.resolve(levelName), new SimpleFileVisitor<>() { | |
// public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
// if (!file.endsWith("session.lock")) { | |
// long lastModified = file.toFile().lastModified(); | |
// if (!CommonConfig.onlyModified() || lastModified - lastSaved > 0) { | |
// com.google.common.io.Files.asByteSource(file.toFile()).copyTo(xzOut); | |
// } | |
// } | |
// | |
// return FileVisitResult.CONTINUE; | |
// } | |
// }); | |
// | |
// xzOut.close(); | |
// outputStream.close(); | |
// | |
// return Files.size(outputFile); | |
// } | |
@Override | |
public String getFileExtension() { | |
return "xz"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment