forked from akka/alpakka
-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
211 additions
and
6 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
99 changes: 99 additions & 0 deletions
99
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,99 @@ | ||
/* | ||
* 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.alpakka.file.ZipArchiveMetadata | ||
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} | ||
|
||
@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
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