//压缩
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
compressor.setInput(input);
compressor.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
byte[] compressedData = bos.toByteArray();
//解压
bos.reset();
bos = new ByteArrayOutputStream(compressedData.length);
Inflater decompressor = new Inflater();
decompressor.setInput(compressedData);
while (!decompressor.finished()) {
try {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
} catch (DataFormatException e) {
}
}
bos.close();
byte[] decompressedData = bos.toByteArray();
分享到:
相关推荐
import java.util.zip.*; public class ZipExample { public static void main(String[] args) throws IOException { // 创建 ZipOutputStream 对象 FileOutputStream fos = new FileOutputStream("example.zip...
import java.util.zip.*; public class UnzipExample { public static void main(String[] args) { try { File file = new File("path_to_your_zip_file.zip"); ZipInputStream zis = new ZipInputStream(new ...
在Java编程语言中,`java.util.InputMismatchException`是一个常见的运行时异常,它通常发生在尝试从数据源(如控制台、文件或数据库)读取数据时,遇到的数据类型与预期的不匹配。在这个特定的场景中,问题出在主线...
在Java编程语言中,`java.util.zip`包提供了对压缩文件格式的支持,如ZIP和GZ等。然而,早期版本的Java在处理包含非ASCII字符(例如中文字符)的文件名时存在一些问题。这是因为ZIP文件格式本身是支持Unicode编码的...
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.io.FileUtils; public class ZipUtil { public static void zipDirectory(String sourceDirPath, String ...
在使用Apache Tomcat服务器时,有时会遇到启动异常的情况,其中一种常见的错误是`java.util.zip.ZipException`。这个异常通常表明在处理ZIP或JAR文件时遇到了问题,可能是因为文件损坏、格式不正确或者无法打开。在...
Tutorialspoint java.util.zip 教程.epub
import java.util.zip.*; public class ZipExample { public static void main(String[] args) throws IOException { File dirToZip = new File("源文件夹路径"); FileOutputStream fos = new FileOutputStream...
import java.util.zip.*; public class Zipper { public void compress(String sourceFilePath, String targetZipFilePath) throws IOException { FileOutputStream fos = new FileOutputStream...
1、文件解压zip通用机制方法、一行代码支持中文; 2、一行代码解决 java.util.zip.ZipInputStream 中文乱码; 3、删除指定路径内的所有文件通用机制方法;
Java API中的`java.util.GregorianCalendar`类是用于处理日期和时间的重要工具,它提供了丰富的功能来满足各种日历操作需求。这个类是基于格里高利历(公历)的,是Java中最常用的日期时间类之一。在给定的压缩包...
在Java中,我们可以使用内置的`java.util.zip`包来处理ZIP文件,但这个包并不支持EXT压缩。如果遇到这个问题,可能需要寻找第三方库,如Apache Ant,这正是标签"java ant"所指向的。 Apache Ant是一个基于Java的...
`ZipInputStream`和`ZipOutputStream`是Java的`java.util.zip`包中的类,分别用于读取和写入ZIP文件。`ZipInputStream`可以从输入流中读取ZIP格式的数据,而`ZipOutputStream`则可以将数据写入ZIP格式的输出流。 ...
CRC(Cyclic Redundancy Check,循环冗余校验)是一种广泛用于数据传输错误...在Java中实现CRC16,可以参考提供的`CRC16.java`源代码,或者利用CRC库如CRCjy-v1.1.zip中的资源,以便更高效地在项目中应用CRC16校验。
总结来说,"zipJava.zip"提供的源码展示了如何利用Java标准库中的`java.util.zip`包来创建ZIP文件。通过创建`ZipOutputStream`,设置`ZipEntry`,读取文件内容并写入,我们可以方便地将多个文件打包成一个压缩文件。...
例如,在Java中,可以使用java.util.zip包,而在Python中,可以使用内置的zipfile模块。对于支持中文文件名,这些库通常已经处理了Unicode编码,以确保正确解析非ASCII字符,包括中文。 在读取ZIP文件时,我们首先...
import java.util.zip.*; public class CreateZip { public static void createZip(String zipFilePath, String[] fileNames) { try (FileOutputStream fos = new FileOutputStream(zipFilePath); ...
博文链接:https://onlyLove.iteye.com/blog/228044
import java.util.zip.*; public class ZipExample { public static void main(String[] args) throws IOException { File fileToZip = new File("Test.java"); FileOutputStream fos = new FileOutputStream(...