Description
Create a zip file containing the given File.
License
Open Source License
Parameter
Parameter | Description |
---|
zipFileName | The path to the zip file. If it doesn't end in .zip, .zip will be added to it. |
toBeZipped | The Path of the file/directory to be zipped. |
Exception
Parameter | Description |
---|
IOException | if there's a problem |
FileNotFoundException | if there's a problem |
Declaration
public static void saveZipFile(String zipFileName, File toBeZipped) throws IOException, FileNotFoundException
Method Source Code
//package com.java2s;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
protected static Logger logger = Logger.getLogger("com.bbn.openmap.util.FileUtils");
/**/*from ww w. j a v a 2 s .c o m*/
* Create a zip file containing the given File.
*
* @param zipFileName The path to the zip file. If it doesn't end in .zip,
* .zip will be added to it.
* @param toBeZipped The Path of the file/directory to be zipped.
* @throws IOException if there's a problem
* @throws FileNotFoundException if there's a problem
*/
public static void saveZipFile(String zipFileName, File toBeZipped) throws IOException, FileNotFoundException {
try {
if (!zipFileName.endsWith(".zip")) {
zipFileName += ".zip";
}
File zipFile = new File(zipFileName);
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zoStream = new ZipOutputStream(fos);
// zoStream.setMethod(ZipOutputStream.STORED);
writeZipEntry(toBeZipped, zoStream, toBeZipped.getParent().length() + 1);
zoStream.close();
} catch (SecurityException se) {
logger.warning("Security Exception caught while creating " + zipFileName);
}
}
/**
* Create a zip file containing the files in the list. The entries will not
* have their parent's file names in their path, they are stored with the
* given file at the root of the zip/jar.
*
* @param zipFileName The path to the zip/jar file.
* @param toBeZipped The List of files to be placed in the zip/jar.
* @throws IOException if there's a problem
* @throws FileNotFoundException if there's a problem
*/
public static void saveZipFile(String zipFileName, List<File> toBeZipped)
throws IOException, FileNotFoundException {
try {
File zipFile = new File(zipFileName);
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(zipFile);
CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32());
ZipOutputStream zoStream = new ZipOutputStream(new BufferedOutputStream(checksum));
// ZipOutputStream zoStream = new ZipOutputStream(fos);
// zoStream.setMethod(ZipOutputStream.STORED);
for (File file : toBeZipped) {
writeZipEntry(file, zoStream, file.getParent().length() + 1);
}
zoStream.close();
} catch (SecurityException se) {
logger.warning("Security Exception caught while creating " + zipFileName);
}
}
/**
* Writes a file to the jar stream.
*
* @param toBeZipped the file to be written
* @param zoStream the stream to write it to, prepared for the
* ZipFile/JarFile
* @param prefixTrimLength The number of characters to trim off the absolute
* path of the file to be zipped. Can be useful to adjust this to
* adjust the directory depth of the entry for when it is unpacked.
* If less than 0, only the file name will be used.
* @throws IOException if there's a problem
*/
public static void writeZipEntry(File toBeZipped, ZipOutputStream zoStream, int prefixTrimLength)
throws IOException {
if (toBeZipped.isDirectory()) {
File[] files = toBeZipped.listFiles();
for (int i = 0; i < files.length; i++) {
writeZipEntry(files[i], zoStream, prefixTrimLength);
}
} else {
if (logger.isLoggable(Level.FINE)) {
logger.fine(toBeZipped + ", " + toBeZipped.getAbsolutePath().substring(prefixTrimLength) + ")");
}
writeZipEntry(toBeZipped, zoStream, prefixTrimLength < 0 ? toBeZipped.getName()
: toBeZipped.getAbsolutePath().substring(prefixTrimLength));
}
}
protected static void writeZipEntry(File fromFile, ZipOutputStream zoStream, String entryName)
throws IOException {
entryName = entryName.replace('\\', '/');
// long size = fromFile.length();
ZipEntry zEntry = new ZipEntry(entryName);
// zEntry.setSize(size);
// zEntry.setCrc(0);// Don't know what it these values are
// right now, but zero works...
zoStream.putNextEntry(zEntry);
FileInputStream fis = new FileInputStream(fromFile);
byte[] bytes = new byte[1024];
int numRead;
// CRC32 checksum = new CRC32();
while ((numRead = fis.read(bytes)) > 0) {
zoStream.write(bytes, 0, numRead);
// checksum.update(bytes, 0, numRead);
}
// zEntry.setCrc(checksum.getValue());
zoStream.closeEntry();
fis.close();
}
}
Related
- saveToFile(String fileName, String fileCode, File dir, String fileExtention)
- saveToFile(String filePath, InputStream in)
- saveToTextFile(String filename, byte[] text)
- SaveToZipSB(File file, String ZippedFile, StringBuilder sb)
- saveZip(String fileName, Map dataMap)