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

A file compression program written in JAVA

王林
王林forward
2024-01-24 12:09:07542browse

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!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete