java
36 lines · 8 steps
Extracting a zip safely in Java
A zip extractor that streams each entry to disk while defending against path-traversal attacks.
Explained by
highlit
1public final class ZipExtractor {
2
3 private final Path targetDir;
4
5 public ZipExtractor(Path targetDir) {
6 this.targetDir = targetDir.toAbsolutePath().normalize();
7 }
8
9 public void extract(Path archive) throws IOException {
10 try (ZipInputStream zis = new ZipInputStream(
11 new BufferedInputStream(Files.newInputStream(archive)))) {
12 ZipEntry entry;
13 while ((entry = zis.getNextEntry()) != null) {
14 Path resolved = resolveSafely(entry.getName());
15 if (entry.isDirectory()) {
16 Files.createDirectories(resolved);
17 } else {
18 Files.createDirectories(resolved.getParent());
19 try (OutputStream out = Files.newOutputStream(resolved,
20 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
21 zis.transferTo(out);
22 }
23 }
24 zis.closeEntry();
25 }
26 }
27 }
28
29 private Path resolveSafely(String entryName) throws IOException {
30 Path resolved = targetDir.resolve(entryName).normalize();
31 if (!resolved.startsWith(targetDir)) {
32 throw new IOException("Blocked path traversal attempt: " + entryName);
33 }
34 return resolved;
35 }
36}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Always validate resolved zip entry paths against the target root to prevent Zip Slip attacks.
- 2Normalizing paths up front makes later containment checks with startsWith reliable.
- 3Try-with-resources on nested streams guarantees handles close even when extraction fails.
Related explainers
java
public class SlidingLogRateLimiter { private final int maxRequests; private final long windowMillis;
How a sliding-log rate limiter works
rate-limiting
concurrency
sliding-window
Advanced
8 steps
java
public final class Slugifier { private static final Pattern NON_LATIN = Pattern.compile("[^\\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
Building a URL slugifier in Java
regex
unicode-normalization
string-processing
Intermediate
8 steps
go
package auth import ( "net/http"
Setting and reading secure session cookies in Go
cookies
session-management
security
Intermediate
6 steps
java
@Repository public interface OrderRepository extends JpaRepository<Order, Long> { @Query("""
Keyset pagination with Spring Data JPA
pagination
cursor
jpa
Advanced
8 steps
java
public record FixedWidthField(String name, int start, int end) { public String extract(String line) { int from = Math.min(start, line.length());
Parsing fixed-width text in Java
records
parsing
streams
Intermediate
7 steps
java
@RestController @RequestMapping("/api/quotes") public class QuoteStreamController {
Streaming market quotes with Spring WebFlux
reactive-streams
backpressure
server-sent-events
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/extracting-a-zip-safely-in-java-explained-java-33d3/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.