use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ObjectUtil method unserialize.
/**
* 反序列化<br>
* 对象必须实现Serializable接口
*
* @param <T> 对象类型
* @param bytes 反序列化的字节码
* @return 反序列化后的对象
*/
@SuppressWarnings("unchecked")
public static <T> T unserialize(byte[] bytes) {
ObjectInputStream ois = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return (T) ois.readObject();
} catch (Exception e) {
throw new UtilException(e);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class XmlUtil method createXml.
// -------------------------------------------------------------------------------------- Create
/**
* 创建XML文档<br>
* 创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,既XML在转为文本的时候才定义编码
*
* @return XML文档
* @since 4.0.8
*/
public static Document createXml() {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = dbf.newDocumentBuilder();
} catch (Exception e) {
throw new UtilException(e, "Create xml document error!");
}
final Document doc = builder.newDocument();
return doc;
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ZipUtil method zip.
/**
* 对文件或文件目录进行压缩
*
* @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
* @param charset 编码
* @param withSrcDir 是否包含被打包目录
* @param srcFiles 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
* @return 压缩文件
* @throws UtilException IO异常
*/
public static File zip(File zipFile, Charset charset, boolean withSrcDir, File... srcFiles) throws UtilException {
validateFiles(zipFile, srcFiles);
try (ZipOutputStream out = getZipOutputStream(zipFile, charset)) {
String srcRootDir;
for (File srcFile : srcFiles) {
// 如果只是压缩一个文件,则需要截取该文件的父目录
srcRootDir = srcFile.getCanonicalPath();
if (srcFile.isFile() || withSrcDir) {
srcRootDir = srcFile.getParent();
}
// 调用递归压缩方法进行目录或文件压缩
zip(srcFile, srcRootDir, out);
out.flush();
}
} catch (IOException e) {
throw new UtilException(e);
}
return zipFile;
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ZipUtil method gzip.
/**
* Gzip压缩文件
*
* @param file 被压缩的文件
* @return 压缩后的字节流
* @throws UtilException IO异常
*/
public static byte[] gzip(File file) throws UtilException {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
GZIPOutputStream gos = null;
BufferedInputStream in;
try {
gos = new GZIPOutputStream(bos);
in = FileUtil.getInputStream(file);
IoUtil.copy(in, gos);
return bos.toByteArray();
} catch (IOException e) {
throw new UtilException(e);
} finally {
IoUtil.close(gos);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ZipUtil method addFile.
/**
* 添加文件流到压缩包,不关闭输入流
*
* @param in 需要压缩的输入流
* @param path 压缩的路径
* @param out 压缩文件存储对象
* @throws UtilException IO异常
*/
private static void addFile(InputStream in, String path, ZipOutputStream out) throws UtilException {
if (null == in) {
return;
}
try {
out.putNextEntry(new ZipEntry(path));
IoUtil.copy(in, out);
} catch (IOException e) {
throw new UtilException(e);
} finally {
closeEntry(out);
}
}
Aggregations