下面列出了java.util.zip.ZipOutputStream#closeEntry ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see io.apicurio.hub.api.codegen.OpenApi2JaxRs#generateAll(io.apicurio.hub.api.codegen.beans.CodegenInfo, java.lang.StringBuilder, java.util.zip.ZipOutputStream)
*/
@Override
protected void generateAll(CodegenInfo info, StringBuilder log, ZipOutputStream zipOutput)
throws IOException {
super.generateAll(info, log, zipOutput);
if (!this.isUpdateOnly()) {
log.append("Generating Dockerfile\r\n");
zipOutput.putNextEntry(new ZipEntry("Dockerfile"));
zipOutput.write(generateDockerfile().getBytes());
zipOutput.closeEntry();
log.append("Generating openshift-template.yml\r\n");
zipOutput.putNextEntry(new ZipEntry("openshift-template.yml"));
zipOutput.write(generateOpenshiftTemplate().getBytes());
zipOutput.closeEntry();
log.append("Generating src/main/resources/META-INF/microprofile-config.properties\r\n");
zipOutput.putNextEntry(new ZipEntry("src/main/resources/META-INF/microprofile-config.properties"));
zipOutput.write(generateMicroprofileConfigProperties().getBytes());
zipOutput.closeEntry();
}
}
public void generate(ZipOutputStream out) {
log.debug("开始处理bizData.data...");
try{
List<Map<String,Object>> list = FoxBpmUtil.getProcessEngine().getModelService().getAllBizObjects();
ObjectMapper objectMapper = new ObjectMapper();
JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
String tmpEntryName = "cache/bizData.data";
ZipEntry zipEntry = new ZipEntry(tmpEntryName);
zipEntry.setMethod(ZipEntry.DEFLATED);// 设置条目的压缩方式
out.putNextEntry(zipEntry);
jsonGenerator.writeObject(list);
out.closeEntry();
log.debug("处理bizData.data文件完毕");
}catch(Exception ex){
log.error("解析bizData.data文件失败!生成zip文件失败!");
throw new FoxBPMException("解析bizData.data文件失败",ex);
}
}
public static void zip(FileObject root, List<FileObject> files, OutputStream out) throws IOException {
String basePath = root.getName().getPath();
Closer closer = Closer.create();
try {
ZipOutputStream zos = new ZipOutputStream(out);
closer.register(zos);
for (FileObject fileToCopy : files) {
ZipEntry zipEntry = zipEntry(basePath, fileToCopy);
zos.putNextEntry(zipEntry);
copyFileContents(fileToCopy, zos);
zos.flush();
zos.closeEntry();
}
} catch (IOException e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
private static void copyToZipStream(File file, ZipEntry entry,
ZipOutputStream zos) throws IOException {
InputStream is = new FileInputStream(file);
try {
zos.putNextEntry(entry);
byte[] arr = new byte[4096];
int read = is.read(arr);
while (read > -1) {
zos.write(arr, 0, read);
read = is.read(arr);
}
} finally {
try {
is.close();
} finally {
zos.closeEntry();
}
}
}
public static void zipFile(String filePath, String zipPath) throws Exception{
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("spy.log");
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(filePath);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
}
private static void addFolderToZip(File rootFolder, File srcFolder, int depth,
ZipOutputStream zip, FileFilter filter) throws IOException {
for (File file : srcFolder.listFiles(filter)) {
if (file.isFile()) {
addFileToZip(rootFolder, file, zip);
continue;
}
if(depth > 0 && file.isDirectory()) {
zip.putNextEntry(new ZipEntry(getRelativePath(file,rootFolder)));
addFolderToZip(rootFolder, srcFolder, depth-1, zip, filter);
zip.closeEntry();
}
}
}
/**
* Zips the given file.
*/
public static File zip(File file) throws IOException {
String outFilename = file.getAbsolutePath() + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
outFilename));
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
out.close();
return new File(outFilename);
}
public String encode(boolean compress, String filename) throws IOException {
byte[] bytes;
if(compress) {
// Zip the content
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
zos.putNextEntry(new ZipEntry(filename));
zos.write(this.content);
zos.closeEntry();
zos.close();
bytes = baos.toByteArray();
} else {
bytes = this.content;
}
return DatatypeConverter.printBase64Binary(bytes);
}
private static File createZipFile() throws Exception {
File zipFile = tempFile();
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = new ZipEntry("firstEntry");
out.putNextEntry(entry);
out.write("fileContents".getBytes(StandardCharsets.UTF_8));
out.closeEntry();
out.close();
return zipFile;
}
/**
* Adds a file to the current zip output stream
*
* @param file the file to be added
* @param zos the current zip output stream
* @throws FileNotFoundException
* @throws IOException
*/
private void zipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException
{
zos.putNextEntry(new ZipEntry(file.getName()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
long bytesRead = 0;
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while((read = bis.read(bytesIn)) != -1)
{
zos.write(bytesIn, 0, read);
bytesRead += read;
}
zos.closeEntry();
}
public void write ( final Document doc, final OutputStream stream ) throws IOException
{
if ( this.compressed )
{
final ZipOutputStream zos = new ZipOutputStream ( stream );
zos.putNextEntry ( new ZipEntry ( this.basename + ".xml" ) );
writeDoc ( doc, zos );
zos.closeEntry ();
zos.finish ();
}
else
{
writeDoc ( doc, stream );
}
}
/**
* Zip it
*
* @param zipFile output ZIP file location
*/
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
try {
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String file : this.fileList) {
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
// remember close it
zos.close();
} catch (IOException e) {
LogUtils.getLogger().log(Level.WARNING, "Couldn't zip the files");
LogUtils.logThrowable(e);
}
}
private File createZip(ZipEntryContext... zipEntryContexts)
{
File zipFile = TempFileProvider.createTempFile(getClass().getSimpleName(), ".zip");
tempFiles.add(zipFile);
byte[] buffer = new byte[BUFFER_SIZE];
try
{
OutputStream out = new BufferedOutputStream(new FileOutputStream(zipFile), BUFFER_SIZE);
ZipOutputStream zos = new ZipOutputStream(out);
for (ZipEntryContext context : zipEntryContexts)
{
ZipEntry zipEntry = new ZipEntry(context.getZipEntryName());
zos.putNextEntry(zipEntry);
InputStream input = context.getEntryContent();
int len;
while ((len = input.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
input.close();
}
zos.closeEntry();
zos.close();
}
catch (IOException ex)
{
fail("couldn't create zip file.");
}
return zipFile;
}
public void copyServletResponse(ZipOutputStream zipOutputStream, HttpServletRequest request, HttpServletResponse response, String resource,
String destinationFileName, Set resources, Set linkSet, String linkFilter) {
long timeStart = new Date().getTime();
try {
IbisServletResponseWrapper ibisHttpServletResponseGrabber = new IbisServletResponseWrapper(response);
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher(resource);
requestDispatcher.include(request, ibisHttpServletResponseGrabber);
String htmlString = ibisHttpServletResponseGrabber.getStringWriter().toString();
ZipEntry zipEntry=new ZipEntry(destinationFileName);
// if (resourceModificationTime!=0) {
// zipEntry.setTime(resourceModificationTime);
// }
zipOutputStream.putNextEntry(zipEntry);
PrintWriter pw = new PrintWriter(zipOutputStream);
pw.print(htmlString);
pw.flush();
zipOutputStream.closeEntry();
if (!resource.startsWith("FileViewerServlet")) {
extractResources(resources, htmlString);
}
if (linkSet!=null) {
followLinks(linkSet, htmlString, linkFilter);
}
} catch (Exception e) {
log.error("Error copying servletResponse", e);
}
long timeEnd = new Date().getTime();
log.debug("dumped file [" + destinationFileName + "] in " + (timeEnd - timeStart) + " msec.");
}
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 {
String path = relativePath + f.getName();
if (!path.equals(JarFile.MANIFEST_NAME)) {
ZipEntry anEntry = new ZipEntry(path);
InputStream is = new FileInputStream(f);
try {
copyToZipStream(is, anEntry, zos);
} finally {
if (is != null) {
is.close();
}
if (zos != null) {
zos.closeEntry();
}
}
}
}
}
}
}
private static byte[] zip(String[] names, byte[] bytes) throws IOException {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ZipOutputStream zippedOut = new ZipOutputStream(bytesOut);
for (String name : names) {
ZipEntry entry = new ZipEntry(name);
zippedOut.putNextEntry(entry);
zippedOut.write(bytes);
zippedOut.closeEntry();
}
zippedOut.close();
return bytesOut.toByteArray();
}
@DesireNoWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void notBug(ZipOutputStream any1, ZipOutputStream any2, ZipEntry anyZipEntry) throws IOException {
any1.putNextEntry(anyZipEntry);
any2.closeEntry();
}
public int process() throws TransformerException, IOException, SAXException {
ZipInputStream zis = new ZipInputStream(input);
final ZipOutputStream zos = new ZipOutputStream(output);
final OutputStreamWriter osw = new OutputStreamWriter(zos);
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
TransformerFactory tf = TransformerFactory.newInstance();
if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) {
return 0;
}
SAXTransformerFactory saxtf = (SAXTransformerFactory) tf;
Templates templates = null;
if (xslt != null) {
templates = saxtf.newTemplates(xslt);
}
// configuring outHandlerFactory
// ///////////////////////////////////////////////////////
EntryElement entryElement = getEntryElement(zos);
ContentHandler outDocHandler = null;
switch (outRepresentation) {
case BYTECODE:
outDocHandler = new OutputSlicingHandler(new ASMContentHandlerFactory(zos), entryElement, false);
break;
case MULTI_XML:
outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw, true), entryElement, true);
break;
case SINGLE_XML:
ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME);
zos.putNextEntry(outputEntry);
outDocHandler = new SAXWriter(osw, false);
break;
}
// configuring inputDocHandlerFactory
// /////////////////////////////////////////////////
ContentHandler inDocHandler;
if (templates == null) {
inDocHandler = outDocHandler;
} else {
inDocHandler = new InputSlicingHandler("class", outDocHandler, new TransformerHandlerFactory(saxtf, templates, outDocHandler));
}
ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(inDocHandler);
if (inDocHandler != null && inRepresentation != SINGLE_XML) {
inDocHandler.startDocument();
inDocHandler.startElement("", "classes", "classes", new AttributesImpl());
}
int i = 0;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
update(ze.getName(), n++);
if (isClassEntry(ze)) {
processEntry(zis, ze, inDocHandlerFactory);
} else {
OutputStream os = entryElement.openEntry(getName(ze));
copyEntry(zis, os);
entryElement.closeEntry();
}
i++;
}
if (inDocHandler != null && inRepresentation != SINGLE_XML) {
inDocHandler.endElement("", "classes", "classes");
inDocHandler.endDocument();
}
if (outRepresentation == SINGLE_XML) {
zos.closeEntry();
}
zos.flush();
zos.close();
return i;
}
private static void initalizeLoggger() {
new File("./log/backup/").mkdirs();
File[] files = new File("log").listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".log");
}
});
if (files != null && files.length > 0) {
byte[] buf = new byte[1024];
try {
String outFilename = "./log/backup/" + new SimpleDateFormat("yyyy-MM-dd HHmmss").format(new Date()) + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
out.setMethod(ZipOutputStream.DEFLATED);
out.setLevel(Deflater.BEST_COMPRESSION);
for (File logFile : files) {
FileInputStream in = new FileInputStream(logFile);
out.putNextEntry(new ZipEntry(logFile.getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
logFile.delete();
}
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
configurator.doConfigure("config/slf4j-logback.xml");
}
catch (JoranException je) {
throw new RuntimeException("[LoggerFactory] Failed to configure loggers, shutting down...", je);
}
}
private static void zip(File input, String destDir, ZipOutputStream z) throws IOException {
if (!input.isDirectory()) {
z.putNextEntry(new ZipEntry((destDir + input.getName())));
FileInputStream inStream = new FileInputStream(input);
byte[] a = new byte[(int) input.length()];
int did = inStream.read(a);
if (did != input.length())
throw new IOException("Impossibile leggere tutto il file " + input.getPath() + " letti solo " + did + " di " + input.length());
z.write(a, 0, a.length);
z.closeEntry();
inStream.close();
input = null;
} else { // recurse
String newDestDir = destDir + input.getName() + "/";
String newInpurPath = input.getPath() + "/";
z.putNextEntry(new ZipEntry(newDestDir));
z.closeEntry();
String[] dirlist = (input.list());
input = null;
for (int i = 0; i < dirlist.length; i++) {
zip(new File(newInpurPath + dirlist[i]), newDestDir, z);
}
}
}