SoFunction
Updated on 2025-04-10

How to compress and decompress files on Android

I won’t say much nonsense, I will just post java code to you. The specific code is as follows:

Java code

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class ZipUtils { 
private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte 
/**
 * Batch compressed files (folder)
 *
 * @param resFileList
 * List of files (folders) to be compressed
 * @param zipFile
 * The generated compressed file
 * @throws IOException
 * Thrown when a compression process error occurs
 */ 
public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException { 
ZipOutputStream zipout = null; 
try { 
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE)); 
for (File resFile : resFileList) { 
zipFile(resFile, zipout, ""); 
} 
} finally { 
if (zipout != null) 
(); 
} 
} 
/**
 * Batch compressed files (folder)
 *
 * @param resFileList
 * List of files (folders) to be compressed
 * @param zipFile
 * The generated compressed file
 * @param comment
 * Commented file comments
 * @throws IOException
 * Thrown when a compression process error occurs
 */ 
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException { 
ZipOutputStream zipout = null; 
try { 
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE)); 
for (File resFile : resFileList) { 
zipFile(resFile, zipout, ""); 
} 
(comment); 
} finally { 
if (zipout != null) 
(); 
} 
} 
/**
 * Unzip a file
 *
 * @param zipFile
 * Compressed file
 * @param folderPath
 * Decompressed target directory
 * @throws IOException
 * Thrown when an error occurs in the decompression process
 */ 
public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException { 
File desDir = new File(folderPath); 
if (!()) { 
(); 
} 
ZipFile zf = new ZipFile(zipFile); 
InputStream in = null; 
OutputStream out = null; 
try { 
for (Enumeration<?> entries = (); ();) { 
ZipEntry entry = ((ZipEntry) ()); 
in = (entry); 
String str = folderPath +  + (); 
str = new String(("8859_1"), HTTP.UTF_8); 
File desFile = new File(str); 
if (!()) { 
File fileParentDir = (); 
if (!()) { 
(); 
} 
(); 
} 
out = new FileOutputStream(desFile); 
byte buffer[] = new byte[BUFF_SIZE]; 
int realLength; 
while ((realLength = (buffer)) > 0) { 
(buffer, 0, realLength); 
} 
} 
} finally { 
if (in != null) 
(); 
if (out != null) 
(); 
} 
} 
/**
 * Unzip the file whose file name contains incoming text
 *
 * @param zipFile
 * Compressed file
 * @param folderPath
 * Destination folder
 * @param nameContains
 * The file matching name passed in
 * @throws ZipException
 * Throw out if the compression format is incorrect
 * @throws IOException
 * Thrown when IO error
 */ 
public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath, String nameContains) throws ZipException, 
IOException { 
ArrayList<File> fileList = new ArrayList<File>(); 
File desDir = new File(folderPath); 
if (!()) { 
(); 
} 
ZipFile zf = new ZipFile(zipFile); 
InputStream in = null; 
OutputStream out = null; 
try { 
for (Enumeration<?> entries = (); ();) { 
ZipEntry entry = ((ZipEntry) ()); 
if (().contains(nameContains)) { 
in = (entry); 
String str = folderPath +  + (); 
str = new String(("8859_1"), HTTP.UTF_8); 
// (AppConstans.UTF_8),"8859_1" output// ("8859_1"),AppConstans.UTF_8 inputFile desFile = new File(str); 
if (!()) { 
File fileParentDir = (); 
if (!()) { 
(); 
} 
(); 
} 
out = new FileOutputStream(desFile); 
byte buffer[] = new byte[BUFF_SIZE]; 
int realLength; 
while ((realLength = (buffer)) > 0) { 
(buffer, 0, realLength); 
} 
(desFile); 
} 
} 
} finally { 
if (in != null) 
(); 
if (out != null) 
(); 
} 
return fileList; 
} 
/**
 * Get a list of files in compressed files
 *
 * @param zipFile
 * Compressed file
 * @return File name in compressed file
 * @throws ZipException
 * Throw out if the compressed file format is incorrect
 * @throws IOException
 * Thrown when an error occurs in the decompression process
 */ 
public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException { 
ArrayList<String> entryNames = new ArrayList<String>(); 
Enumeration<?> entries = getEntriesEnumeration(zipFile); 
while (()) { 
ZipEntry entry = ((ZipEntry) ()); 
(new String(getEntryName(entry).getBytes(HTTP.UTF_8), "8859_1")); 
} 
return entryNames; 
} 
/**
 * Get the compressed file object in the compressed file to obtain its properties
 *
 * @param zipFile
 * Compressed file
 * @return Return a list of compressed files
 * @throws ZipException
 * Throw out if the compressed file format is incorrect
 * @throws IOException
 * Thrown when IO operation is incorrect
 */ 
public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException, IOException { 
ZipFile zf = new ZipFile(zipFile); 
return (); 
} 
/**
 * Get comments on compressed file objects
 *
 * @param entry
 * Compressed file object
 * @return Commented file object comment
 * @throws UnsupportedEncodingException
 */ 
public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException { 
return new String(().getBytes(HTTP.UTF_8), "8859_1"); 
} 
/**
 * Get the name of the compressed file object
 *
 * @param entry
 * Compressed file object
 * @return The name of the compressed file object
 * @throws UnsupportedEncodingException
 */ 
public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException { 
return new String(().getBytes(HTTP.UTF_8), "8859_1"); 
} 
/**
 * Compressed file
 *
 * @param resFile
 * Files (folders) that need to be compressed
 * @param zipout
 * Compressed target file
 * @param rootpath
 * Compressed file path
 * @throws FileNotFoundException
 * Thrown when the file cannot be found
 * @throws IOException
 * Thrown when a compression process error occurs
 */ 
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException { 
rootpath = rootpath + (().length() == 0 ? "" : ) + (); 
rootpath = new String(("8859_1"), HTTP.UTF_8); 
BufferedInputStream in = null; 
try { 
if (()) { 
File[] fileList = (); 
for (File file : fileList) { 
zipFile(file, zipout, rootpath); 
} 
} else { 
byte buffer[] = new byte[BUFF_SIZE]; 
in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE); 
(new ZipEntry(rootpath)); 
int realLength; 
while ((realLength = (buffer)) != -1) { 
(buffer, 0, realLength); 
} 
(); 
(); 
(); 
} 
} finally { 
if (in != null) 
(); 
// if (zipout != null); 
// (); 
} 
} 
} 

The code ends here. I will introduce so much to you about the full content of Android's compression and decompression files. I hope it can help you!