The list of methods to do Zip Files are organized into topic(s).
void
addFilesToZip(File zipFile, File[] files, String[] fileNames) add Files To Zip
File tempFile = new File(zipFile.getAbsoluteFile() + ".temp");
if (!zipFile.renameTo(tempFile)) {
throw new RuntimeException("could not rename");
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
...
void
addFileToZip(File file, String entryName, ZipOutputStream zos) Inserts a file into a zipstream using the specified entryName.
byte[] buffer = new byte[8192];
int read = 0;
FileInputStream in = new FileInputStream(file);
ZipEntry entry = new ZipEntry(entryName);
zos.putNextEntry(entry);
while (-1 != (read = in.read(buffer))) {
zos.write(buffer, 0, read);
in.close();
void
addFileToZip(File file, String parentFolderName, ZipOutputStream zip) Adds a specified File (that is not a folder) to the specified ZipOutputStream , the File being located in the specified parent folder.
String parent = parentFolderName == null || parentFolderName.trim().isEmpty() ? ""
: parentFolderName.trim() + "/";
if (file.exists()) {
zip.putNextEntry(new ZipEntry(parent + file.getName()));
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[16096];
int count = 0;
while ((count = inputStream.read(buffer)) != -1) {
...
void
addFileToZip(File file, ZipOutputStream zos) Adds a file to the current zip output stream
zos.putNextEntry(new ZipEntry(file.getName()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
zos.write(bytesIn, 0, read);
zos.closeEntry();
...
void
addFileToZip(File in, File parent, ZipOutputStream out) read a file out to the zip stream
String path = in.getCanonicalPath();
String parentPath = parent.getCanonicalPath();
if (!path.startsWith(parentPath)) {
throw new Error("not parent: " + parentPath + " of " + path);
} else {
path = path.substring(1 + parentPath.length());
path = path.replace('\\', '/');
ZipEntry entry = new ZipEntry(path);
entry.setTime(in.lastModified());
out.putNextEntry(entry);
InputStream input = null;
try {
input = new FileInputStream(in);
byte[] buf = new byte[1024];
int count;
while (0 < (count = input.read(buf, 0, buf.length))) {
out.write(buf, 0, count);
} finally {
if (null != input)
input.close();
void
addFileToZip(File root, File file, ZipOutputStream zos) add File To Zip
FileInputStream fis = new FileInputStream(file);
String filePath = file.getCanonicalPath();
String zipFilePath = filePath.substring(root.getCanonicalPath().length() + 1, filePath.length());
ZipEntry zipEntry = new ZipEntry(zipFilePath);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
...