本文整理了Java中java.util.zip.ZipOutputStream.putNextEntry()
方法的一些代码示例,展示了ZipOutputStream.putNextEntry()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipOutputStream.putNextEntry()
方法的具体详情如下:
包路径:java.util.zip.ZipOutputStream
类名称:ZipOutputStream
方法名:putNextEntry
[英]Writes entry information to the underlying stream. Data associated with the entry can then be written using write(). After data is written closeEntry() must be called to complete the writing of the entry to the underlying stream.
[中]将条目信息写入基础流。然后,可以使用write()写入与条目关联的数据。写入数据后,必须调用closeEntry()以完成对底层流的条目写入。
canonical example by Tabnine
public void zipFile(File srcFile, File zipFile) throws IOException {
try (FileInputStream fis = new FileInputStream(srcFile);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
}
}
代码示例来源:origin: pxb1988/dex2jar
@Override
public OutputStream _newOutputStream() throws IOException {
ZipEntry e = new ZipEntry(path.substring(1));
zos.putNextEntry(e);
return new FilterOutputStream(zos) {
@Override
public void close() throws IOException {
zos.closeEntry();
}
};
}
代码示例来源:origin: apache/incubator-druid
public static void makeEvilZip(File outputFile) throws IOException
{
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outputFile));
ZipEntry zipEntry = new ZipEntry("../../../../../../../../../../../../../../../tmp/evil.txt");
zipOutputStream.putNextEntry(zipEntry);
byte[] output = StringUtils.toUtf8("evil text");
zipOutputStream.write(output);
zipOutputStream.closeEntry();
zipOutputStream.close();
}
}
代码示例来源:origin: SonarSource/sonarqube
private static void doZip(String entryName, InputStream in, ZipOutputStream out) throws IOException {
ZipEntry entry = new ZipEntry(entryName);
out.putNextEntry(entry);
IOUtils.copy(in, out);
out.closeEntry();
}
代码示例来源:origin: apache/ignite
/**
* Archives specified file into zip archive.
*
* @param file File to be archived.
* @return Byte array representing zip archive.
* @throws IOException In case of input/output exception.
*/
private byte[] zipFile(File file) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) {
IOUtils.copy(in, zos);
}
}
return baos.toByteArray();
}
代码示例来源:origin: gocd/gocd
@Override
protected void handleFile(File file, int depth, Collection results) throws IOException {
if (excludeFiles.contains(file.getAbsolutePath())) {
return;
}
zipStream.putNextEntry(new ZipEntry(fromRoot(file)));
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
IOUtils.copy(in, zipStream);
}
}
代码示例来源:origin: gocd/gocd
void addToZip(ZipPath path, File srcFile, ZipOutputStream zip, boolean excludeRootDir) throws IOException {
if (srcFile.isDirectory()) {
addFolderToZip(path, srcFile, zip, excludeRootDir);
} else {
byte[] buff = new byte[4096];
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile))) {
ZipEntry zipEntry = path.with(srcFile).asZipEntry();
zipEntry.setTime(srcFile.lastModified());
zip.putNextEntry(zipEntry);
int len;
while ((len = inputStream.read(buff)) > 0) {
zip.write(buff, 0, len);
}
}
}
}
代码示例来源:origin: Tencent/tinker
/**
* add zip entry
*
* @param zipOutputStream
* @param zipEntry
* @param inputStream
* @throws Exception
*/
public static void addZipEntry(ZipOutputStream zipOutputStream, ZipEntry zipEntry, InputStream inputStream) throws Exception {
try {
zipOutputStream.putNextEntry(zipEntry);
byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB];
int length = -1;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
zipOutputStream.write(buffer, 0, length);
zipOutputStream.flush();
}
} catch (ZipException e) {
// do nothing
} finally {
StreamUtil.closeQuietly(inputStream);
zipOutputStream.closeEntry();
}
}
代码示例来源:origin: square/wire
private void writeFile(ZipOutputStream out, String file, String content) throws IOException {
out.putNextEntry(new ZipEntry(file));
out.write(content.getBytes(UTF_8));
}
}
代码示例来源:origin: apache/hive
private static void copyToZipStream(InputStream is, ZipEntry entry, ZipOutputStream zos)
throws IOException {
zos.putNextEntry(entry);
IOUtils.copy(is, zos);
is.close();
zos.closeEntry();
}
代码示例来源:origin: stackoverflow.com
StringBuilder sb = new StringBuilder();
sb.append("Test String");
File f = new File("d:\\test.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
ZipEntry e = new ZipEntry("mytext.txt");
out.putNextEntry(e);
byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
代码示例来源:origin: facebook/stetho
private void addFiles(ZipOutputStream output, byte[] buf, File[] files) throws IOException {
for (File file : files) {
if (file.isDirectory()) {
addFiles(output, buf, file.listFiles());
} else {
output.putNextEntry(
new ZipEntry(
relativizePath(
getBaseDir(mContext).getParentFile(),
file)));
FileInputStream input = new FileInputStream(file);
try {
copy(input, output, buf);
} finally {
input.close();
}
}
}
}
代码示例来源:origin: pxb1988/dex2jar
private boolean createDir0(String path) throws IOException {
int x = path.lastIndexOf('/', path.length() - 2);
if (x > 0) {
String n = path.substring(0, x + 1);
createDir0(n);
}
if (!path.contains(path)) {
files.add(path);
ZipEntry zipEntry = new ZipEntry(path);
zos.putNextEntry(zipEntry);
zos.closeEntry();
return true;
}
return false;
}
代码示例来源:origin: spotbugs/spotbugs
@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void bug3(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException {
any.write(anyValue);
any.putNextEntry(anyZipEntry);
any.closeEntry();
}
代码示例来源:origin: apache/incubator-druid
private static void createNewZipEntry(ZipOutputStream out, File file) throws IOException
{
log.info("Creating new ZipEntry[%s]", file.getName());
out.putNextEntry(new ZipEntry(file.getName()));
}
代码示例来源:origin: Meituan-Dianping/Robust
protected void zipFile(byte[] classBytesArray, ZipOutputStream zos, String entryName) {
try {
ZipEntry entry = new ZipEntry(entryName);
zos.putNextEntry(entry);
zos.write(classBytesArray, 0, classBytesArray.length);
zos.closeEntry();
zos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
代码示例来源:origin: iBotPeaches/Apktool
private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException {
// First, copy the contents from the existing outFile:
Enumeration<? extends ZipEntry> entries = inputFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = new ZipEntry(entries.nextElement());
// We can't reuse the compressed size because it depends on compression sizes.
entry.setCompressedSize(-1);
outputFile.putNextEntry(entry);
// No need to create directory entries in the final apk
if (! entry.isDirectory()) {
BrutIO.copy(inputFile, outputFile, entry);
}
outputFile.closeEntry();
}
}
代码示例来源:origin: spotbugs/spotbugs
@NoWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void notBug(ZipOutputStream any, ZipEntry anyZipEntry, byte[] anyBytes) throws IOException {
any.putNextEntry(anyZipEntry);
any.write(anyBytes);
any.closeEntry();
}
代码示例来源:origin: gocd/gocd
@Override
protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
if (!directory.getAbsolutePath().equals(configDirectory)) {
ZipEntry e = new ZipEntry(fromRoot(directory) + "/");
zipStream.putNextEntry(e);
}
return true;
}
代码示例来源:origin: pxb1988/dex2jar
public void dumpZip(Path exFile, String[] originalArgs) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(exFile))) {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos, StandardCharsets.UTF_8));
zos.putNextEntry(new ZipEntry("summary.txt"));
dumpTxt0(writer, originalArgs);
zos.closeEntry();
zos.flush();
}
}
内容来源于网络,如有侵权,请联系作者删除!