Java Code Examples for java.util.zip.ZipOutputStream#write()
The following examples show how to use
java.util.zip.ZipOutputStream#write() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipUtil.java From translationstudio8 with GNU General Public License v2.0 | 6 votes |
/** * 递归调用,压缩文件夹和子文件夹的所有文件 * @param out * @param f * @param base * @throws Exception */ private static void zipFiles(ZipOutputStream out, File f, String base) throws IOException { if (f.isDirectory()) { File[] fl = f.listFiles(); // out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zipFiles(out, fl[i], base + fl[i].getName());// 递归压缩子文件夹 } } else { out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; // System.out.println(base); while ((b = in.read()) != -1) { out.write(b); } in.close(); } }
Example 2
Source File: ZipUtils.java From letv with Apache License 2.0 | 6 votes |
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); } }
Example 3
Source File: FileUtils.java From FireFiles with Apache License 2.0 | 6 votes |
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(); } }
Example 4
Source File: OldAndroidZipFileTest.java From j2objc with Apache License 2.0 | 6 votes |
static void createCompressedZip(OutputStream bytesOut) throws IOException { ZipOutputStream out = new ZipOutputStream(bytesOut); try { int i; for (i = 0; i < 3; i++) { byte[] input = makeSampleFile(i); ZipEntry newEntry = new ZipEntry("file-" + i); if (i != 1) { newEntry.setComment("this is file " + i); } out.putNextEntry(newEntry); out.write(input, 0, input.length); out.closeEntry(); } out.setComment("This is a lovely compressed archive!"); } finally { out.close(); } }
Example 5
Source File: DocumentExecutionResource.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
private static void addToZipFile(String filePath, String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException { File file = new File(filePath + "/" + fileName); FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); }
Example 6
Source File: WorldMapExportTask.java From SuntimesWidget with GNU General Public License v3.0 | 5 votes |
@Override protected boolean export(Context context, BufferedOutputStream out) throws IOException { if (bitmaps != null && bitmaps.size() > 0) { if (zippedOutput) // write entire bitmap array to zip { ZipOutputStream zippedOut = new ZipOutputStream(out); try { int c = 0; while (!isCancelled() && (!bitmaps.isEmpty() || waitForFrames)) { byte[] bitmap = bitmaps.poll(); if (bitmap != null) { ZipEntry entry = new ZipEntry(c + imageExt); entry.setMethod(ZipEntry.DEFLATED); zippedOut.putNextEntry(entry); zippedOut.write(bitmap); zippedOut.flush(); c++; } } } catch (IOException e) { Log.e("ExportTask", "Error writing zip file: " + e); throw e; } finally { zippedOut.close(); } } else { out.write(bitmaps.peek()); out.flush(); } return true; } else return true; }
Example 7
Source File: ZipFolder.java From dal with Apache License 2.0 | 5 votes |
public void zipIt(String zipFile) { generateFileList(new File(zipFolder)); byte[] buffer = new byte[1024]; try { FileOutputStream fos = new FileOutputStream(new File(generatePath, zipFile)); ZipOutputStream zos = new ZipOutputStream(fos); for (String file : this.fileList) { ZipEntry ze = new ZipEntry(file); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(this.zipFolder + 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 ex) { ex.printStackTrace(); } }
Example 8
Source File: Bug62060793TestDataGenerator.java From bazel with Apache License 2.0 | 5 votes |
private static void writeToZipFile(ZipOutputStream outZip, String entryName, byte[] content) throws IOException { ZipEntry result = new ZipEntry(entryName); result.setTime(0L); outZip.putNextEntry(result); outZip.write(content); outZip.closeEntry(); }
Example 9
Source File: Zip.java From joyqueue with Apache License 2.0 | 5 votes |
@Override public void compress(final byte[] buf, final int offset, final int size, final OutputStream out) throws IOException { if (buf == null || out == null) { return; } ZipOutputStream zos = new ZipOutputStream(out); try { zos.putNextEntry(new ZipEntry("0")); zos.write(buf, offset, size); zos.closeEntry(); } finally { zos.close(); } }
Example 10
Source File: TestConfigSetsAPI.java From lucene-solr with Apache License 2.0 | 5 votes |
private static void zip(File directory, File zipfile) throws IOException { URI base = directory.toURI(); Deque<File> queue = new LinkedList<File>(); queue.push(directory); OutputStream out = new FileOutputStream(zipfile); ZipOutputStream zout = new ZipOutputStream(out); try { while (!queue.isEmpty()) { directory = queue.pop(); for (File kid : directory.listFiles()) { String name = base.relativize(kid.toURI()).getPath(); if (kid.isDirectory()) { queue.push(kid); name = name.endsWith("/") ? name : name + "/"; zout.putNextEntry(new ZipEntry(name)); } else { zout.putNextEntry(new ZipEntry(name)); InputStream in = new FileInputStream(kid); try { byte[] buffer = new byte[1024]; while (true) { int readCount = in.read(buffer); if (readCount < 0) { break; } zout.write(buffer, 0, readCount); } } finally { in.close(); } zout.closeEntry(); } } } } finally { zout.close(); } }
Example 11
Source File: JavaGWTDocumentCreator.java From jolie with GNU Lesser General Public License v2.1 | 5 votes |
public void ConvertTypes( TypeDefinition typeDefinition, ZipOutputStream zipStream, String portName ) throws IOException { StringBuilder builderHeaderclass = new StringBuilder(); builderHeaderclass.append( "package " ).append( namespace ).append( ".types;\n" ); importsCreate( builderHeaderclass, typeDefinition ); convertClass( typeDefinition, builderHeaderclass ); String namespaceDir = namespace.replaceAll( "\\.", "/" ); ZipEntry zipEntry = new ZipEntry( namespaceDir + "/types/" + typeDefinition.id() + ".java" ); zipStream.putNextEntry( zipEntry ); byte[] bb = builderHeaderclass.toString().getBytes(); zipStream.write( bb, 0, bb.length ); zipStream.closeEntry(); }
Example 12
Source File: ZipFileTest.java From j2objc with Apache License 2.0 | 5 votes |
public void test_sameNamesDifferentCase() throws Exception { // Create a final File tempFile = File.createTempFile("smdc", "zip"); try { // Create a zip file with multiple entries with same text and different // capitalization FileOutputStream tempFileStream = new FileOutputStream(tempFile); ZipOutputStream zipOutputStream = new ZipOutputStream(tempFileStream); zipOutputStream.putNextEntry(new ZipEntry("test.txt")); zipOutputStream.write(new byte[2]); zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry("Test.txt")); zipOutputStream.write(new byte[2]); zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry("TEST.TXT")); zipOutputStream.write(new byte[2]); zipOutputStream.closeEntry(); zipOutputStream.close(); tempFileStream.close(); ZipFile zipFile = new ZipFile(tempFile); final List<String> names = new ArrayList<>(); zipFile.stream().forEach((ZipEntry entry) -> names.add(entry.getName())); assertEquals(Arrays.asList("test.txt", "Test.txt", "TEST.TXT"), names); } finally { tempFile.delete(); } }
Example 13
Source File: TestFileUtil.java From hadoop with Apache License 2.0 | 5 votes |
@Test (timeout = 30000) public void testUnZip() throws IOException { // make sa simple zip setupDirs(); // make a simple tar: final File simpleZip = new File(del, FILE); OutputStream os = new FileOutputStream(simpleZip); ZipOutputStream tos = new ZipOutputStream(os); try { ZipEntry ze = new ZipEntry("foo"); byte[] data = "some-content".getBytes("UTF-8"); ze.setSize(data.length); tos.putNextEntry(ze); tos.write(data); tos.closeEntry(); tos.flush(); tos.finish(); } finally { tos.close(); } // successfully untar it into an existing dir: FileUtil.unZip(simpleZip, tmp); // check result: assertTrue(new File(tmp, "foo").exists()); assertEquals(12, new File(tmp, "foo").length()); final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog"); regularFile.createNewFile(); assertTrue(regularFile.exists()); try { FileUtil.unZip(simpleZip, regularFile); assertTrue("An IOException expected.", false); } catch (IOException ioe) { // okay } }
Example 14
Source File: BackupManager.java From HeartBeat with Apache License 2.0 | 5 votes |
public static String backupAll(Context context) { String dbPath = backupSD(context); if (dbPath == null) return null; try { File zipFile = FileUtils.generateBackupAllZip(context); FileOutputStream dest = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER_SIZE]; File f = FileUtils.getImageDir(); ArrayList<File> files = new ArrayList<>(Arrays.asList(f.listFiles())); files.add(new File(dbPath)); for (File file : files) { FileInputStream fi = new FileInputStream(file); BufferedInputStream origin = new BufferedInputStream(fi, BUFFER_SIZE); ZipEntry entry = new ZipEntry(file.getName()); out.putNextEntry(entry); int count; while ((count = origin.read(data,0, BUFFER_SIZE)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); return zipFile.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
Example 15
Source File: AarGeneratorActionTest.java From bazel with Apache License 2.0 | 5 votes |
private void writeClassesJar() throws IOException { final ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(classes.toFile())); for (Map.Entry<String, String> file : classesToWrite.entrySet()) { ZipEntry entry = new ZipEntry(file.getKey()); zout.putNextEntry(entry); zout.write(file.getValue().getBytes(UTF_8)); zout.closeEntry(); } zout.close(); classes.toFile().setLastModified(AarGeneratorAction.DEFAULT_TIMESTAMP); }
Example 16
Source File: BusyBoxZipHelper.java From BusyBox with Apache License 2.0 | 5 votes |
private static void zipFile(String path, ZipOutputStream out, String zipPath) throws IOException { final byte[] buffer = new byte[1024 * 4]; File file = new File(path); if (file.isFile()) { FileInputStream in = new FileInputStream(file.getAbsolutePath()); try { if (zipPath.startsWith(File.separator)) { zipPath = zipPath.replaceFirst(File.separator, ""); } out.putNextEntry(new ZipEntry(zipPath)); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); } catch (ZipException e) { e.printStackTrace(); } finally { IoUtils.closeQuietly(in); } } else { String[] files = file.list(); if (files != null && files.length > 0) { for (String filepath : files) { String relPath = new File(zipPath).getParent(); if (relPath == null) relPath = ""; zipFile(path + "/" + filepath, out, relPath + file.getName() + "/"); } } } }
Example 17
Source File: TaobaoInstantRunDex.java From atlas with Apache License 2.0 | 5 votes |
public static void zipPatch(File file, File dexFile) throws IOException { BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(dexFile)); byte[] BUFFER = new byte[4096]; FileOutputStream fOutputStream = new FileOutputStream(file); ZipOutputStream zoutput = new ZipOutputStream(fOutputStream); ZipEntry zEntry = new ZipEntry(dexFile.getName()); zoutput.putNextEntry(zEntry); int len; while ((len = inputStream.read(BUFFER)) > 0) { zoutput.write(BUFFER, 0, len); } zoutput.closeEntry(); zoutput.close(); inputStream.close(); }
Example 18
Source File: MathMachineII.java From ET_Redux with Apache License 2.0 | 4 votes |
/** * * @param variables * @param rootDirectoryName * @param createNewExpressionFilesXML * @throws IOException */ public static void outputExpressionFilesXML(// ValueModel[][] variables, String rootDirectoryName, boolean createNewExpressionFilesXML) throws IOException { // drive ExpTreeII output from here if (createNewExpressionFilesXML) { MathMachineII.expFiles = new HashMap<>(); MathMachineII.rootDirectoryName = rootDirectoryName; } File rootDirectory = new File("." + File.separator + rootDirectoryName+File.separator); if (rootDirectory.exists()) { if (createNewExpressionFilesXML) { // find and delete all .xml files File[] expressionFilesXML = rootDirectory.listFiles(new FractionXMLFileFilter()); for (File f : expressionFilesXML) { f.delete(); } } } else { rootDirectory.mkdir(); } FileOutputStream outputDirectoryDest = new FileOutputStream(rootDirectory+".zip"); ZipOutputStream output = new ZipOutputStream(outputDirectoryDest); int BUFFER = 2048; byte[] data = new byte[BUFFER]; for (ValueModel[] vms : variables) { for (ValueModel valueModel : vms) { FileInputStream input = new FileInputStream("." + File.separator + rootDirectoryName + File.separator + createPresentationFileVMs(valueModel.getValueTree(), valueModel.differenceValueCalcs())); ZipEntry entry = new ZipEntry(valueModel.getName()); output.putNextEntry(entry); BufferedInputStream in = new BufferedInputStream(input, BUFFER); int count; while ((count = in.read(data, 0, BUFFER)) != -1) { output.write(data, 0, count); } output.closeEntry(); in.close(); } } output.close(); }
Example 19
Source File: Schedule.java From Quelea with GNU General Public License v3.0 | 4 votes |
/** * Write this schedule to a file. * * @return true if the write was successful, false otherwise. */ public synchronized boolean writeToFile() { if (file == null) { return false; } try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file), Charset.forName("UTF-8")); final int BUFFER = 2048; byte data[] = new byte[BUFFER]; try { zos.putNextEntry(new ZipEntry("schedule.xml")); zos.write(getXML().getBytes("UTF8")); zos.closeEntry(); if (QueleaProperties.get().getEmbedMediaInScheduleFile()) { Set<String> entries = new HashSet<>(); for (Displayable displayable : displayables) { for (File displayableFile : displayable.getResources()) { if (displayableFile.exists()) { String zipPath = "resources/" + displayableFile.getName(); if (!entries.contains(zipPath)) { entries.add(zipPath); ZipEntry entry = new ZipEntry(zipPath); zos.putNextEntry(entry); FileInputStream fi = new FileInputStream(displayableFile); try (BufferedInputStream origin = new BufferedInputStream(fi, BUFFER)) { int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { zos.write(data, 0, count); } zos.closeEntry(); } } } } } } modified = false; return true; } finally { zos.close(); } } catch (IOException ex) { LOGGER.log(Level.WARNING, "Couldn't write the schedule to file", ex); return false; } }
Example 20
Source File: ZipUtils.java From signature with MIT License | 2 votes |
/** * 递归压缩方法 * * @param sourceFile 源文件 * @param zos zip输出流 * @param name 压缩后的名称 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * <p> * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { byte[] buf = new byte[BUFFER_SIZE]; if (sourceFile.isFile()) { // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { // 需要保留原来的文件结构时,需要对空文件夹进行处理 if (KeepDirStructure) { // 空文件夹的处理 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不需要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 判断是否需要保留原来的文件结构 if (KeepDirStructure) { // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } }