io.github.nichetoolkit.rest.helper.ZipHelper Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.helper;
import io.github.nichetoolkit.rest.constant.UtilConstants;
import io.github.nichetoolkit.rest.error.often.FileCreateException;
import io.github.nichetoolkit.rest.error.often.ZipErrorException;
import java.io.*;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* ZipHelper
* The zip helper class.
* @author Cyan ([email protected])
* @since Jdk1.8
*/
public class ZipHelper {
/**
* zipFile
* The zip file method.
* @param zipPath {@link java.lang.String} The zip path parameter is String
type.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param file {@link java.io.File} The file parameter is File
type.
* @return {@link java.io.File} The zip file return object is File
type.
* @throws ZipErrorException {@link io.github.nichetoolkit.rest.error.often.ZipErrorException} The zip error exception is ZipErrorException
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.lang.String
* @see java.io.File
* @see java.lang.SuppressWarnings
* @see io.github.nichetoolkit.rest.error.often.ZipErrorException
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
@SuppressWarnings("Duplicates")
public static File zipFile(String zipPath, String filename, File file) throws ZipErrorException, FileCreateException {
String zipFilePath = zipPath.concat(File.separator).concat(filename)
.concat(UtilConstants.SUFFIX_REGEX).concat(UtilConstants.ZIP_SUFFIX);
if (filename.endsWith(UtilConstants.SUFFIX_REGEX.concat(UtilConstants.ZIP_SUFFIX))) {
zipFilePath = zipPath.concat(File.separator).concat(filename);
}
File zipFile = FileHelper.createFile(zipFilePath);
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
zipOutputStream.setComment(filename);
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
int temp;
while ((temp = inputStream.read()) != -1) {
zipOutputStream.write(temp);
}
}
} catch (IOException exception) {
throw new ZipErrorException(exception.getMessage());
}
return zipFile;
}
/**
* zipFiles
* The zip files method.
* @param zipPath {@link java.lang.String} The zip path parameter is String
type.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param zipFiles {@link java.util.List} The zip files parameter is List
type.
* @return {@link java.io.File} The zip files return object is File
type.
* @throws ZipErrorException {@link io.github.nichetoolkit.rest.error.often.ZipErrorException} The zip error exception is ZipErrorException
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.lang.String
* @see java.util.List
* @see java.io.File
* @see java.lang.SuppressWarnings
* @see io.github.nichetoolkit.rest.error.often.ZipErrorException
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
@SuppressWarnings("Duplicates")
public static File zipFiles(String zipPath, String filename, List zipFiles) throws ZipErrorException, FileCreateException {
if (zipFiles.size() == 1) {
return zipFile(zipPath,filename,zipFiles.stream().findFirst().get());
}
String zipFilePath = zipPath.concat(File.separator).concat(filename)
.concat(UtilConstants.SUFFIX_REGEX).concat(UtilConstants.ZIP_SUFFIX);
if (filename.endsWith(UtilConstants.SUFFIX_REGEX.concat(UtilConstants.ZIP_SUFFIX))) {
zipFilePath = zipPath.concat(File.separator).concat(filename);
}
File zipFile = FileHelper.createFile(zipFilePath);
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
zipOutputStream.setComment(filename);
for (File file : zipFiles) {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
int temp;
while ((temp = inputStream.read()) != -1) {
zipOutputStream.write(temp);
}
}
}
} catch (IOException exception) {
throw new ZipErrorException(exception.getMessage());
}
return zipFile;
}
/**
* gzip
* The gzip method.
* @param data byte The data parameter is byte
type.
* @return byte The gzip return object is byte
type.
* @throws ZipErrorException {@link io.github.nichetoolkit.rest.error.often.ZipErrorException} The zip error exception is ZipErrorException
type.
* @see io.github.nichetoolkit.rest.error.often.ZipErrorException
*/
public static byte[] gzip(byte[] data) throws ZipErrorException {
byte[] bytes;
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
gzipOutputStream.write(data);
gzipOutputStream.finish();
bytes = byteArrayOutputStream.toByteArray();
} catch (IOException exception) {
throw new ZipErrorException(exception.getMessage());
}
return bytes;
}
/**
* ungzip
* The ungzip method.
* @param data byte The data parameter is byte
type.
* @return byte The ungzip return object is byte
type.
* @throws ZipErrorException {@link io.github.nichetoolkit.rest.error.often.ZipErrorException} The zip error exception is ZipErrorException
type.
* @see io.github.nichetoolkit.rest.error.often.ZipErrorException
*/
public static byte[] ungzip(byte[] data) throws ZipErrorException {
byte[] bytes;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
byte[] buf = new byte[1024];
int num;
while ((num = gzipInputStream.read(buf, 0, buf.length)) != -1) {
byteArrayOutputStream.write(buf, 0, num);
}
bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.flush();
} catch (IOException exception) {
throw new ZipErrorException(exception.getMessage());
}
return bytes;
}
}