= ZipUtils
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

Utilities to zip and unzip data.

== API

[source,java]
.ZipUtils.java
----
class ZipUtils {
  Stream<ZipEntryDataSource> streamZipEntries(DataSource zippedSource, ZipOptions zipOptions)     // <.>
  Stream<ZipEntryDataSource> streamZipEntries(DataSource zippedSource)     // <.>
  Optional<ZipEntryDataSource> firstZipEntry(DataSource zippedSource, ZipOptions zipOptions)     // <.>
  Optional<ZipEntryDataSource> firstZipEntry(DataSource zippedSource)     // <.>
  byte[] zipToBytes(Stream<ZipEntryDataSource> entryStream)
  void writeTo(Stream<ZipEntryDataSource> entryStream, DataSink dataSink)
  EntryBuilder zipEntryBuilder()     // <.>
}
----

<.> xref:#streamZipEntries_DataSource_ZipOptions[streamZipEntries(DataSource, ZipOptions)]
+
--
Returns a _Stream_ of _ZipEntryDataSource_ , buffered in memory, which allows consumption even after the underlying zipped xref:refguide:commons:index/io/DataSource.adoc[DataSource] was closed.
--
<.> xref:#streamZipEntries_DataSource[streamZipEntries(DataSource)]
+
--
Shortcut for _streamZipEntries(zippedSource, ZipOptions.builder().build())_
--
<.> xref:#firstZipEntry_DataSource_ZipOptions[firstZipEntry(DataSource, ZipOptions)]
+
--
Optionally the first zip-entry as _ZipEntryDataSource_ , based on whether an entry exists.
--
<.> xref:#firstZipEntry_DataSource[firstZipEntry(DataSource)]
+
--
Shortcut for _firstZipEntry(zippedSource, ZipOptions.builder().build())_
--
<.> xref:#zipEntryBuilder_[zipEntryBuilder()]
+
--
typical example:
--

== Members

[#streamZipEntries_DataSource_ZipOptions]
=== streamZipEntries(DataSource, ZipOptions)

Returns a _Stream_ of _ZipEntryDataSource_ , buffered in memory, which allows consumption even after the underlying zipped xref:refguide:commons:index/io/DataSource.adoc[DataSource] was closed.

[#streamZipEntries_DataSource]
=== streamZipEntries(DataSource)

Shortcut for _streamZipEntries(zippedSource, ZipOptions.builder().build())_

[#firstZipEntry_DataSource_ZipOptions]
=== firstZipEntry(DataSource, ZipOptions)

Optionally the first zip-entry as _ZipEntryDataSource_ , based on whether an entry exists.

[#firstZipEntry_DataSource]
=== firstZipEntry(DataSource)

Shortcut for _firstZipEntry(zippedSource, ZipOptions.builder().build())_

[#zipEntryBuilder_]
=== zipEntryBuilder()

typical example:

----
 _var builder = ZipUtils.zipEntryBuilder();
for (Map.Entry entry : schemaMap.entrySet()) {
    var namespaceUri = entry.getKey();
    var schemaText = entry.getValue();
    builder.addAsUtf8(zipEntryNameFor(namespaceUri), schemaText);_ 
return Blob.of(fileName, CommonMimeType.ZIP, builder.toBytes());
}

----

----
----
