/*
 * Copyright 2013-2016 consulo.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package consulo.packaging.impl.elements;

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.packaging.impl.elements.ArchivePackagingElement;
import consulo.packaging.elements.ArchivePackageWriter;
import javax.annotation.Nonnull;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author VISTALL
 * @since 16:02/18.06.13
 */
public class ZipArchivePackagingElement extends ArchivePackagingElement {
  public static class ZipArchivePackageWriter implements ArchivePackageWriter<ZipOutputStream> {
    public static final ZipArchivePackageWriter INSTANCE = new ZipArchivePackageWriter();

    @Nonnull
    @Override
    public ZipOutputStream createArchiveObject(@Nonnull File tempFile) throws IOException {
      final BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
      return new ZipOutputStream(outputStream);
    }

    @Override
    public void addDirectory(@Nonnull ZipOutputStream zipOutputStream, @Nonnull String relativePath) throws IOException {
      ZipEntry e = new ZipEntry(relativePath);
      e.setMethod(ZipEntry.STORED);
      e.setSize(0);
      e.setCrc(0);

      zipOutputStream.putNextEntry(e);
      zipOutputStream.closeEntry();
    }

    @Override
    public void addFile(@Nonnull ZipOutputStream zipOutputStream, @Nonnull InputStream stream, @Nonnull String relativePath, long fileLength, long lastModified)
            throws IOException {
      ZipEntry e = new ZipEntry(relativePath);
      e.setTime(lastModified);
      e.setSize(fileLength);

      zipOutputStream.putNextEntry(e);
      FileUtil.copy(stream, zipOutputStream);
      zipOutputStream.closeEntry();
    }

    @Override
    public void
    close(@Nonnull ZipOutputStream zipOutputStream) throws IOException {
      zipOutputStream.close();
    }
  }

  public ZipArchivePackagingElement() {
    super(ZipArchiveElementType.getInstance());
  }

  public ZipArchivePackagingElement(@Nonnull String archiveFileName) {
    super(ZipArchiveElementType.getInstance(), archiveFileName);
  }

  @Override
  public ArchivePackageWriter<?> getPackageWriter() {
    return ZipArchivePackageWriter.INSTANCE;
  }
}

@@ -801,16 +801,17 @@
 Writer;%0A
+%0A
 import j
@@ -835,17 +835,16 @@
 onnull;%0A
-%0A
 import j
@@ -2036,20 +2036,8 @@
 ied)
-%0A           
  thr
@@ -2319,20 +2319,16 @@
 lic void
-%0A   
  close(@

remove spaces