-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
file/src/main/scala/akka/stream/alpakka/file/impl/archive/ZipReaderSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.file.impl.archive | ||
|
||
import akka.NotUsed | ||
import akka.annotation.InternalApi | ||
import akka.stream.{Attributes, Outlet, SourceShape} | ||
import akka.stream.scaladsl.Source | ||
import akka.stream.stage.{GraphStage, GraphStageLogic, OutHandler} | ||
import akka.util.ByteString | ||
|
||
import java.io.{File, FileInputStream} | ||
import java.util.zip.{ZipEntry, ZipInputStream} | ||
|
||
case class ZipArchiveMetadata(name: String) | ||
|
||
@InternalApi class ZipEntrySource(n: ZipArchiveMetadata, f: File, chunkSize: Int) | ||
extends GraphStage[SourceShape[ByteString]] { | ||
private val out = Outlet[ByteString]("flowOut") | ||
override val shape: SourceShape[ByteString] = | ||
SourceShape(out) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) { | ||
val zis = new ZipInputStream(new FileInputStream(f)) | ||
var entry: ZipEntry = null | ||
val data = new Array[Byte](chunkSize) | ||
|
||
def seek() = { | ||
while ({ | ||
entry = zis.getNextEntry() | ||
entry != null && entry.getName != n.name | ||
}) { | ||
zis.closeEntry() | ||
} | ||
} | ||
|
||
setHandler( | ||
out, | ||
new OutHandler { | ||
override def onPull(): Unit = { | ||
if (entry == null) { | ||
seek() | ||
if (entry == null) { | ||
failStage(new Exception("After a seek the part is not found")) | ||
} | ||
} | ||
|
||
val c = zis.read(data, 0, chunkSize) | ||
if (c == -1) { | ||
completeStage() | ||
} else { | ||
push(out, ByteString.fromArray(data, 0, c)) | ||
} | ||
} | ||
} | ||
) | ||
|
||
override def postStop(): Unit = { | ||
super.postStop() | ||
zis.close() | ||
} | ||
} | ||
} | ||
|
||
@InternalApi class ZipSource(f: File, chunkSize: Int) | ||
extends GraphStage[SourceShape[(ZipArchiveMetadata, Source[ByteString, NotUsed])]] { | ||
private val out = Outlet[(ZipArchiveMetadata, Source[ByteString, NotUsed])]("flowOut") | ||
override val shape: SourceShape[(ZipArchiveMetadata, Source[ByteString, NotUsed])] = | ||
SourceShape(out) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) { | ||
val zis = new ZipInputStream(new FileInputStream(f)) | ||
|
||
setHandler( | ||
out, | ||
new OutHandler { | ||
override def onPull(): Unit = { | ||
val e = zis.getNextEntry | ||
if (e != null) { | ||
val n = ZipArchiveMetadata(e.getName) | ||
zis.closeEntry() | ||
push(out, n -> Source.fromGraph(new ZipEntrySource(n, f, chunkSize))) | ||
} else { | ||
zis.close() | ||
completeStage() | ||
} | ||
} | ||
} | ||
) | ||
|
||
override def postStop(): Unit = { | ||
super.postStop() | ||
zis.close() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters