Files
timi-compress/src/test/java/test/GzipTest.java
Timi 829c7f0184
Some checks failed
CI/CD / build-deploy (pull_request) Failing after 10s
v0.0.4
2026-04-07 16:52:13 +08:00

83 lines
2.5 KiB
Java

package test;
import com.imyeyu.compress.CompressType;
import com.imyeyu.compress.GZipCompressor;
import com.imyeyu.compress.GZipDecompressor;
import com.imyeyu.io.IO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author 夜雨
* @version 2024-06-30 17:54
*/
public class GzipTest {
@Test
public void testStaticCompress() throws Exception {
GZipCompressor compressor = GZipCompressor.from("testSrc").toFile("testOut/test.gz");
Assertions.assertNotNull(compressor);
}
@Test
public void testChainCompress() throws Exception {
File out = IO.file("testOut/test-chain.gz");
AtomicInteger fileCount = new AtomicInteger();
AtomicReference<Double> progress = new AtomicReference<>(0D);
GZipCompressor compressor = GZipCompressor.from("testSrc");
Assertions.assertSame(
compressor,
compressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toFile(out)
);
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
}
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.gz");
GZipCompressor.from("testSrc").toFile(in);
File out = IO.dir("testOutDe");
CompressType.from(in).toPath(out.getAbsolutePath());
}
@Test
public void testCompressToStream() throws Exception {
try (OutputStream os = IO.getOutputStream(IO.file("testOut/test.tar.gz"))) {
CompressType.GZIP.compressFrom("testSrc").toStream(os);
}
}
@Test
public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.GZIP.compressFrom("testSrc").toStream(outputStream);
bytes = outputStream.toByteArray();
}
File out = IO.dir("testOutDeStream/gzip");
AtomicInteger fileCount = new AtomicInteger();
AtomicReference<Double> progress = new AtomicReference<>(0D);
GZipDecompressor decompressor = GZipDecompressor.from(new ByteArrayInputStream(bytes));
Assertions.assertSame(
decompressor,
decompressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toPath(out.getAbsolutePath())
);
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
}
}