use of java.util.zip.ZipOutputStream in project atlas by alibaba.
the class ZipUtils method removeZipEntry.
public static boolean removeZipEntry(File file, Pattern pattern, File targetFile) throws FileNotFoundException, IOException {
byte[] buffer = new byte[1024];
java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)));
BufferedOutputStream bo = new BufferedOutputStream(out);
InputStream inputStream;
Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
String name = zipEntry.getName();
if (pattern.matcher(name).find()) {
continue;
}
out.putNextEntry(zipEntry);
inputStream = zipFile.getInputStream(zipEntry);
write(inputStream, out, buffer);
bo.flush();
}
closeQuitely(zipFile);
closeQuitely(out);
closeQuitely(bo);
return true;
}
use of java.util.zip.ZipOutputStream in project atlas by alibaba.
the class JarSplitUtils method removeFilesFromJar.
/**
* 移除指定jar文件中的class
* @param inJar
* @param removeClasses
*/
public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException {
if (null == removeClasses || removeClasses.isEmpty()) {
return;
}
File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp");
File outParentFolder = outJar.getParentFile();
if (!outParentFolder.exists()) {
outParentFolder.mkdirs();
}
FileOutputStream fos = new FileOutputStream(outJar);
ZipOutputStream jos = new ZipOutputStream(fos);
final byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(inJar);
ZipInputStream zis = new ZipInputStream(fis);
try {
// loop on the entries of the jar file package and put them in the final jar
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// do not take directories or anything inside a potential META-INF folder.
if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
continue;
}
String name = entry.getName();
String className = getClassName(name);
if (removeClasses.contains(className)) {
continue;
}
JarEntry newEntry;
// Preserve the STORED method of the input entry.
if (entry.getMethod() == JarEntry.STORED) {
newEntry = new JarEntry(entry);
} else {
// Create a new entry so that the compressed len is recomputed.
newEntry = new JarEntry(name);
}
// add the entry to the jar archive
jos.putNextEntry(newEntry);
// read the content of the entry from the input stream, and write it into the archive.
int count;
while ((count = zis.read(buffer)) != -1) {
jos.write(buffer, 0, count);
}
// close the entries for this file
jos.closeEntry();
zis.closeEntry();
}
} finally {
zis.close();
}
fis.close();
jos.close();
FileUtils.deleteQuietly(inJar);
FileUtils.moveFile(outJar, inJar);
}
use of java.util.zip.ZipOutputStream in project atlas by alibaba.
the class JarSplitUtils method splitZip.
public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException {
if (outputFile.exists()) {
FileUtils.deleteQuietly(outputFile);
}
if (null == includeEnties || includeEnties.size() < 1) {
return;
}
FileOutputStream fos = new FileOutputStream(outputFile);
ZipOutputStream jos = new ZipOutputStream(fos);
final byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(inputFile);
ZipInputStream zis = new ZipInputStream(fis);
try {
// loop on the entries of the jar file package and put them in the final jar
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// do not take directories or anything inside a potential META-INF folder.
if (entry.isDirectory()) {
continue;
}
String name = entry.getName();
if (!includeEnties.contains(name)) {
continue;
}
JarEntry newEntry;
// Preserve the STORED method of the input entry.
if (entry.getMethod() == JarEntry.STORED) {
newEntry = new JarEntry(entry);
} else {
// Create a new entry so that the compressed len is recomputed.
newEntry = new JarEntry(name);
}
// add the entry to the jar archive
jos.putNextEntry(newEntry);
// read the content of the entry from the input stream, and write it into the archive.
int count;
while ((count = zis.read(buffer)) != -1) {
jos.write(buffer, 0, count);
}
// close the entries for this file
jos.closeEntry();
zis.closeEntry();
}
} finally {
zis.close();
}
fis.close();
jos.close();
}
use of java.util.zip.ZipOutputStream in project atlas by alibaba.
the class ZipUtils method addFileAndDirectoryToZip.
/**
* 指定文件压缩成一个zip包,主要给solib发布使用
*
* @param output
* @param srcDir
* @throws Exception
*/
public static void addFileAndDirectoryToZip(File output, File srcDir) throws Exception {
if (output.isDirectory()) {
throw new IOException("This is a directory!");
}
if (!output.getParentFile().exists()) {
output.getParentFile().mkdirs();
}
if (!output.exists()) {
output.createNewFile();
}
List fileList = getSubFiles(srcDir);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
}
zos.close();
}
use of java.util.zip.ZipOutputStream in project atlas by alibaba.
the class ZipUtils method addFileToZipFile.
/**
* 增加文件到zip文件
*
* @param zipFile
* @param file
* @param destPath 要放的路径
* @param overwrite 是否覆盖
* @throws IOException
*/
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath, boolean overwrite) throws IOException {
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
ZipEntry entry = zin.getNextEntry();
boolean addFile = true;
while (entry != null) {
boolean addEntry = true;
String name = entry.getName();
if (StringUtils.equalsIgnoreCase(name, destPath)) {
if (overwrite) {
addEntry = false;
} else {
addFile = false;
}
}
if (addEntry) {
ZipEntry zipEntry = null;
if (ZipEntry.STORED == entry.getMethod()) {
zipEntry = new ZipEntry(entry);
} else {
zipEntry = new ZipEntry(name);
}
out.putNextEntry(zipEntry);
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
if (addFile) {
InputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
ZipEntry zipEntry = new ZipEntry(destPath);
out.putNextEntry(zipEntry);
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Close the streams
zin.close();
out.close();
}
Aggregations