compress(OutputStream output, Map<String, InputStream> inputs) 추가

master
mjkhan21 8 months ago
parent ad015986ee
commit 091a5049b0

@ -3,19 +3,22 @@ package cokr.xit.base.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.AbstractComponent;
/** /
* <p> *.zip , *.zip .
* @author mjkhan
*/
public class ZIP {
public class ZIP extends AbstractComponent {
/** zip .
* @param zipPath zip
* @param targets
@ -42,10 +45,32 @@ public class ZIP {
return new File(zipPath);
} catch(Exception e) {
throw Assert.runtimeException(e);
throw runtimeException(e);
}
}
/**
* @param output OutputStream
* @param inputs InputStream
* <ul><li>key - </li>
* <li>value - InputStream</li>
* </ul>
*/
public void compress(OutputStream output, Map<String, InputStream> inputs) {
try (ZipOutputStream zout = new ZipOutputStream(output);) {
inputs.forEach((filename, input) -> {
try {
compress(zout, filename, input);
} catch (Exception e) {
throw runtimeException(e);
}
});
} catch (Exception e) {
throw runtimeException(e);
}
}
private void compress(ZipOutputStream zout, File file, String filename) throws Exception {
if (file.isHidden()) return;
@ -58,7 +83,9 @@ public class ZIP {
compress(zout, child, filename + "/" + child.getName());
}
} else
try (FileInputStream fis = new FileInputStream(file);) {
compress(zout, filename, new FileInputStream(file));
/*
try (InputStream fis = new FileInputStream(file);) {
zout.putNextEntry(new ZipEntry(filename));
byte[] bytes = new byte[1024];
int length;
@ -66,6 +93,18 @@ public class ZIP {
zout.write(bytes, 0, length);
}
}
*/
}
private void compress(ZipOutputStream zout, String filename, InputStream input) throws Exception {
try (InputStream stream = input) {
zout.putNextEntry(new ZipEntry(filename));
byte[] bytes = new byte[1024];
int length;
while((length = stream.read(bytes)) >= 0) {
zout.write(bytes, 0, length);
}
}
}
/** zip .
@ -79,7 +118,7 @@ public class ZIP {
) {
decompress(zis, destDir);
} catch (Exception e) {
throw Assert.runtimeException(e);
throw runtimeException(e);
}
}

@ -0,0 +1,52 @@
package cokr.xit.base.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
public class ZIPTest {
private static final String
imgDir = "src/test/resources/images",
resultDir = "src/test/resources/results";
@Test
void compressDir() {
new ZIP().compress(resultDir + "/img-dir.zip", imgDir);
}
@Test
void compressFiles() {
List<String> paths = Stream.of(new File(imgDir).listFiles())
.map(file -> file.getAbsolutePath())
.toList();
new ZIP().compress(resultDir + "/img-files.zip", paths.toArray(new String[paths.size()]));
}
@Test
void compressInputStreams() {
Map<String, InputStream> fileMap = Stream.of(new File(imgDir).listFiles())
.collect(Collectors.toMap(
file -> file.getName(),
file -> {
try {
return new FileInputStream(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
));
try (FileOutputStream out = new FileOutputStream(new File(resultDir + "/inputstreams.zip"))) {
new ZIP().compress(out, fileMap);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB

Loading…
Cancel
Save