| // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| package com.android.tools.r8.utils; |
| |
| import java.io.IOException; |
| import java.nio.file.Files; |
| import java.nio.file.Path; |
| import java.nio.file.StandardOpenOption; |
| import java.util.Set; |
| import java.util.zip.ZipEntry; |
| import java.util.zip.ZipOutputStream; |
| |
| public class ZipFileOutputSink extends FileSystemOutputSink { |
| |
| private final ZipOutputStream outputStream; |
| |
| public ZipFileOutputSink(Path outputPath, InternalOptions options) throws IOException { |
| super(options); |
| outputStream = new ZipOutputStream( |
| Files.newOutputStream(outputPath, StandardOpenOption.CREATE, |
| StandardOpenOption.TRUNCATE_EXISTING)); |
| } |
| |
| @Override |
| public void writeDexFile(byte[] contents, Set<String> classDescriptors, int fileId) |
| throws IOException { |
| writeToZipFile(getOutputFileName(fileId), contents); |
| } |
| |
| @Override |
| public void writeDexFile(byte[] contents, Set<String> classDescriptors, String primaryClassName) |
| throws IOException { |
| writeToZipFile(getOutputFileName(primaryClassName), contents); |
| } |
| |
| @Override |
| public void close() throws IOException { |
| outputStream.close(); |
| } |
| |
| private synchronized void writeToZipFile(String outputPath, byte[] content) throws IOException { |
| ZipEntry zipEntry = new ZipEntry(outputPath); |
| zipEntry.setSize(content.length); |
| outputStream.putNextEntry(zipEntry); |
| outputStream.write(content); |
| outputStream.closeEntry(); |
| } |
| } |