Home > Computer Tutorials > Computer Knowledge > A file compression program written in JAVA

A file compression program written in JAVA

王林
Release: 2024-01-24 12:09:07
forward
579 people have browsed it

A file compression program written in JAVA

A JAVA ZIP compression program

In fact, it is nothing more than compressing the file addresses you specify one by one according to the recursive method.

out.putNextEntry(new ZipEntry(XXX)); Here is the content you want to compress,

For example: if it is a folder, then out.putNextEntry(new ZipEntry (folder name "/"));

If it is the content in the folder, then: out.putNextEntry(new ZipEntry (folder name "/" folder name));

Actually, there are still some problems with the program above, and it cannot meet your expectations. Let me help you modify it:

The for loop should be modified to this:

for (int i = 0; i

javaCompress File into zip

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("d:\\test.zip"));

String test1="test1";

String test2="test2";

byte[] bytes1 = test1.getBytes("UTF-8");

byte[] bytes2 = test2.getBytes("UTF-8");

ZipEntry z1 = new ZipEntry("test1.txt");

zos.putNextEntry(z1);

zos.write(bytes1);

ZipEntry z2 = new ZipEntry("text2.txt");

zos.putNextEntry(z2);

zos.write(bytes2);

zos.closeEntry();

zos.close();

//The stream can be obtained by yourself

//Java default package does not support Chinese (garbled characters)

//Use apache's ZipOutputStream for zip compression

Can this solve your problem?

The above is the detailed content of A file compression program written in JAVA. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template