本文整理汇总了Java中java.util.zip.ZipOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java ZipOutputStream.write方法的具体用法?Java ZipOutputStream.write怎么用?Java ZipOutputStream.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.ZipOutputStream
的用法示例。
在下文中一共展示了ZipOutputStream.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compressFile
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private static void compressFile(String currentDir, ZipOutputStream zout, File[] files) throws Exception {
byte[] buffer = new byte[1024];
for (File fi : files) {
if (fi.isDirectory()) {
compressFile(currentDir + "/" + fi.getName(), zout, fi.listFiles());
continue;
}
ZipEntry ze = new ZipEntry(currentDir + "/" + fi.getName());
FileInputStream fin = new FileInputStream(fi.getPath());
zout.putNextEntry(ze);
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
}
示例2: zipFile
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
public static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException {
String rootpath2 = new String(new StringBuilder(String.valueOf(rootpath)).append(rootpath.trim().length() == 0 ? "" : File.separator).append(resFile.getName()).toString().getBytes("8859_1"), "GB2312");
if (resFile.isDirectory()) {
for (File file : resFile.listFiles()) {
zipFile(file, zipout, rootpath2);
}
return;
}
byte[] buffer = new byte[1048576];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), 1048576);
zipout.putNextEntry(new ZipEntry(rootpath2));
while (true) {
int realLength = in.read(buffer);
if (realLength == -1) {
in.close();
zipout.flush();
zipout.closeEntry();
return;
}
zipout.write(buffer, 0, realLength);
}
}
示例3: addManifest
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private void addManifest(ByteArrayOutputStream manifest, ZipOutputStream zipout) throws IOException {
if (manifest.size() > 0) {
zipout.putNextEntry(new ZipEntry(MANIFEST_FILE_NAME));
byte[] manifestBytes = manifest.toByteArray();
if (images != null && images.size() > 0) {
StringBuilder manifestBuilder = new StringBuilder(new String(manifestBytes, UTF8));
for (Map.Entry<String, Image> img : images.entrySet()) {
int p = manifestBuilder.indexOf(MANIFEST_END_TAG);
StringBuilder sb = new StringBuilder(MANIFEST_ENTRY_1);
sb.append(img.getValue().getFormat().toString().toLowerCase());
sb.append(MANIFEST_ENTRY_2).append(img.getKey()).append(MANIFEST_ENTRY_3);
manifestBuilder.insert(p, sb.toString());
}
manifestBytes = manifestBuilder.toString().getBytes(UTF8);
}
zipout.write(manifestBytes);
}
}
示例4: zipIt
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* Zip it
*/
public void zipIt(){
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(OUTPUT_ZIP_FILE);
ZipOutputStream zos = new ZipOutputStream(fos);
//System.out.println("Output to Zip : " + zipFile);
for(String file : this.fileList){
//System.out.println("File Added : " + file);
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER.getAbsolutePath() + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
//System.out.println("Done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
示例5: compress
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
*
* @param map key压缩条目路径 value重命名
* @param zipPath 压缩文件路径
* @return
* @throws IOException
*/
public static boolean compress(Map<String, String> map, String zipPath) throws IOException {
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
for (Map.Entry<String, String> entry : map.entrySet()) {
File file = new File(entry.getKey());
if (file.exists()) {
InputStream inputStream = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(entry.getValue());
zipOutputStream.putNextEntry(zipEntry);
int len = inputStream.read();
while (len != -1) {
zipOutputStream.write(len);
len = inputStream.read();
}
inputStream.close();
}
}
zipOutputStream.close();
return true;
}
示例6: compressFile
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private void compressFile(String currentDir, ZipOutputStream zout, File[] files) throws Exception {
byte[] buffer = new byte[1024];
for (File fi : files) {
if (fi.isDirectory()) {
compressFile(currentDir + "/" + fi.getName(), zout, fi.listFiles());
continue;
}
publishProgress(fi.getName());
publishProgress("+0");
ZipEntry ze = new ZipEntry(currentDir + "/" + fi.getName());
FileInputStream fin = new FileInputStream(fi.getPath());
zout.putNextEntry(ze);
int length;
long readData=0;
final long totalFileLength=fi.length();
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
readData+=length;
doProgress(totalFileLength,readData);
}
zout.closeEntry();
fin.close();
}
}
示例7: addToArchive
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* @param inputDir Input Directory
* @param zos Zip Output Stream
* @throws IOException
*/
private static void addToArchive(File inputDir, ZipOutputStream zos) throws IOException {
File[] fileList = FileUtil.getFileListFromDirectory(inputDir.getPath());
byte[] buffer = new byte[BUFFER_SIZE];
for (File element : fileList) {
if (element.isDirectory()) {
zos.putNextEntry(new ZipEntry(element.getName()));
addToArchive(element, zos);
continue;
}
try (FileInputStream fis = new FileInputStream(element.getPath())) {
log.info("Adding: " + element.getPath() + " to log bundle");
zos.putNextEntry(new ZipEntry(element.getPath()));
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
}
}
}
示例8: zipFile
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* 压缩文件
*
* @param resFile 需要压缩的文件(夹)
* @param zipout 压缩的目的文件
* @param rootpath 压缩的文件路径
* @throws FileNotFoundException 找不到文件时抛出
* @throws IOException 当压缩过程出错时抛出
*/
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
throws FileNotFoundException, IOException {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath.getBytes(), "utf-8");
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),
BUFF_SIZE);
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
zipout.write(buffer, 0, realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
}
示例9: extract
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo, String extractedFilePrefix) throws IOException, FileNotFoundException {
Throwable th;
InputStream in = apk.getInputStream(dexFile);
File tmp = File.createTempFile(extractedFilePrefix, EXTRACTED_SUFFIX, extractTo.getParentFile());
Log.i(TAG, "Extracting " + tmp.getPath());
try {
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmp)));
try {
ZipEntry classesDex = new ZipEntry("classes.dex");
classesDex.setTime(dexFile.getTime());
out.putNextEntry(classesDex);
byte[] buffer = new byte[16384];
for (int length = in.read(buffer); length != -1; length = in.read(buffer)) {
out.write(buffer, 0, length);
}
out.closeEntry();
out.close();
Log.i(TAG, "Renaming to " + extractTo.getPath());
if (tmp.renameTo(extractTo)) {
closeQuietly(in);
tmp.delete();
return;
}
throw new IOException("Failed to rename \"" + tmp.getAbsolutePath() + "\" to \"" + extractTo.getAbsolutePath() + "\"");
} catch (Throwable th2) {
th = th2;
ZipOutputStream zipOutputStream = out;
closeQuietly(in);
tmp.delete();
throw th;
}
} catch (Throwable th3) {
th = th3;
closeQuietly(in);
tmp.delete();
throw th;
}
}
示例10: addEntry
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
public static void addEntry(ZipOutputStream os, String path, byte[] content) throws IOException {
ZipEntry entry = new ZipEntry(path);
entry.setSize(content.length);
os.putNextEntry(entry);
os.write(content);
os.closeEntry();
}
示例11: addEntry
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private void addEntry(ZipOutputStream zos, String theClasspathPrefix, String theFileName) throws IOException {
ourLog.info("Adding {} to test zip", theFileName);
zos.putNextEntry(new ZipEntry("SnomedCT_Release_INT_20160131_Full/Terminology/" + theFileName));
byte[] byteArray = IOUtils.toByteArray(getClass().getResourceAsStream(theClasspathPrefix + theFileName));
Validate.notNull(byteArray);
zos.write(byteArray);
zos.closeEntry();
}
示例12: encodeResponse
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
public static byte[] encodeResponse(Response response) {
long sequence = response.getSequence();
int resultCode = response.getResultCode();
String dataJson = response.getData() == null ? "" : response.getData().toJSONString();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(bos);
try {
zipOutputStream.putNextEntry(new ZipEntry("sequence"));
zipOutputStream.write(Bytes.longToBytes(sequence));
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new ZipEntry("resultCode"));
zipOutputStream.write(Bytes.int2Bytes(resultCode));
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new ZipEntry("data"));
zipOutputStream.write(dataJson.getBytes("UTF-8"));
zipOutputStream.closeEntry();
} catch (IOException e) {
throw new ServerException("Error encoding response", e);
}
return Base64.getEncoder().encode(bos.toByteArray());
}
示例13: shouldCreateValidZipContent
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@Test
public void shouldCreateValidZipContent() throws Exception {
byte[] f1 = newRandomBytes(10000);
byte[] f2 = newRandomBytes(10000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(bos);
zipOutputStream.putNextEntry(new UtcAdjustedZipEntry("a/b/c"));
zipOutputStream.write(f1);
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new UtcAdjustedZipEntry("a/b/c/d/"));
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new UtcAdjustedZipEntry("d/e/f"));
zipOutputStream.write(f2);
zipOutputStream.closeEntry();
zipOutputStream.flush();
zipOutputStream.close();
byte[] expected = bos.toByteArray();
List<DynamicZipInputStream.Entry> entries = new ArrayList<DynamicZipInputStream.Entry>();
entries.add(newEntry("a/b/c", f1));
entries.add(newEntry("a/b/c/d/", null));
entries.add(newEntry("d/e/f", f2));
DynamicZipInputStream inputStream = new DynamicZipInputStream(entries);
bos.reset();
FileCopyUtils.copy(inputStream, bos);
byte[] actual = bos.toByteArray();
assertThat(actual, is(equalTo(expected)));
}
示例14: extract
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo, String extractedFilePrefix) throws IOException, FileNotFoundException {
Throwable th;
InputStream in = apk.getInputStream(dexFile);
File tmp = File.createTempFile(extractedFilePrefix, EXTRACTED_SUFFIX, extractTo.getParentFile());
Log.i(TAG, "Extracting " + tmp.getPath());
try {
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmp)));
try {
ZipEntry classesDex = new ZipEntry("classes.dex");
classesDex.setTime(dexFile.getTime());
out.putNextEntry(classesDex);
byte[] buffer = new byte[16384];
for (int length = in.read(buffer); length != -1; length = in.read(buffer)) {
out.write(buffer, 0, length);
}
out.closeEntry();
out.close();
Log.i(TAG, "Renaming to " + extractTo.getPath());
if (tmp.renameTo(extractTo)) {
closeQuietly(in);
tmp.delete();
return;
}
throw new IOException("Failed to rename \"" + tmp.getAbsolutePath() + "\" to \"" + extractTo.getAbsolutePath() + a.e);
} catch (Throwable th2) {
th = th2;
ZipOutputStream zipOutputStream = out;
closeQuietly(in);
tmp.delete();
throw th;
}
} catch (Throwable th3) {
th = th3;
closeQuietly(in);
tmp.delete();
throw th;
}
}
示例15: writeZipStream
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private void writeZipStream(final ZipOutputStream stream, final String log) {
if (stream == null) {
return;
}
// write to zip output stream
try {
stream.write(log.getBytes());
stream.write(Unix.LINE_SEPARATOR.getBytes());
} catch (IOException e) {
LOGGER.warn("Log cannot write : " + log);
}
}