Description
Zips a local directory, recursively, into a ZIP stream.
License
Apache License
Parameter
Parameter | Description |
---|
dir | directory to ZIP. |
relativePath | basePath in the ZIP for the files, normally "/". |
zos | the ZIP output stream to ZIP the directory. |
Exception
Declaration
public static void zipDir(File dir, String relativePath,
ZipOutputStream zos) throws IOException
Method Source Code
//package com.java2s;
/**//from w w w . ja v a2 s.co m
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
public class Main {
/**
* Zips a local directory, recursively, into a ZIP stream.
*
* @param dir directory to ZIP.
* @param relativePath basePath in the ZIP for the files, normally "/".
* @param zos the ZIP output stream to ZIP the directory.
* @throws java.io.IOException thrown if the directory could not be zipped.
*/
public static void zipDir(File dir, String relativePath,
ZipOutputStream zos) throws IOException {
zipDir(dir, relativePath, zos, true);
zos.close();
}
private static void zipDir(File dir, String relativePath,
ZipOutputStream zos, boolean start) throws IOException {
String[] dirList = dir.list();
for (String aDirList : dirList) {
File f = new File(dir, aDirList);
if (!f.isHidden()) {
if (f.isDirectory()) {
if (!start) {
ZipEntry dirEntry = new ZipEntry(relativePath
+ f.getName() + "/");
zos.putNextEntry(dirEntry);
zos.closeEntry();
}
String filePath = f.getPath();
File file = new File(filePath);
zipDir(file, relativePath + f.getName() + "/", zos,
false);
} else {
ZipEntry anEntry = new ZipEntry(relativePath
+ f.getName());
zos.putNextEntry(anEntry);
InputStream is = new FileInputStream(f);
byte[] arr = new byte[4096];
int read = is.read(arr);
while (read > -1) {
zos.write(arr, 0, read);
read = is.read(arr);
}
is.close();
zos.closeEntry();
}
}
}
}
}
Related
- doZip(String inFilePath, String outFilePath)
- doZipFile(ZipOutputStream zipOut, File file, String dirPath)
- zip(File rootDir, String zipPath)
- zip(File rootDir, String zipPath)
- zipDir(File dir, String classPackage, String zipFile)
- zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
- zipDir(File dir, ZipOutputStream zos, String prefix)
- zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
- zipDir(File sourceDir, File destFile)