Implementation:SeleniumHQ Selenium Zip
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Infrastructure |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Zip is a utility class that compresses files or directories into Base64-encoded zip archives and extracts Base64-encoded or stream-based zip archives to the filesystem.
Description
Zip is a utility class in the org.openqa.selenium.io package that provides static methods for zipping and unzipping files. The zip method takes a file or directory, recursively compresses it into a zip archive, and returns the result as a Base64-encoded string. The unzip methods accept either a Base64-encoded string or an InputStream and extract the contents to a specified output directory. The class preserves file timestamps during both compression and extraction, uses a 16384-byte buffer for extraction, and includes a path traversal security check (zip slip protection) to prevent entries from being written outside the target directory. Temporary directory creation for extraction is supported through integration with TemporaryFilesystem.
Usage
Use Zip for file transfer operations in the WebDriver protocol, such as uploading files to remote browsers (files are zipped and Base64-encoded for transport over the wire), downloading files from remote sessions, or managing file extensions and profiles that need to be packaged for transfer between Selenium client and server.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/io/Zip.java
Signature
public class Zip {
public static String zip(File input) throws IOException
public static File unzipToTempDir(String source, String prefix, String suffix)
throws IOException
public static void unzip(String source, File outputDir) throws IOException
public static File unzipToTempDir(InputStream source, String prefix, String suffix)
throws IOException
public static void unzip(InputStream source, File outputDir) throws IOException
public static void unzipFile(File output, InputStream zipStream, String name)
throws IOException
}
Import
import org.openqa.selenium.io.Zip;
I/O Contract
| Method | Input | Output | Description |
|---|---|---|---|
zip(File input) |
input: file or directory to compress |
String -- Base64-encoded zip archive |
Recursively zips the input file/directory. Preserves file timestamps and converts backslashes to forward slashes in entry names. |
unzipToTempDir(String source, String prefix, String suffix) |
source: Base64-encoded zip string; prefix/suffix: temp dir naming |
File -- path to the created temporary directory |
Decodes the Base64 string, creates a temporary directory via TemporaryFilesystem, and extracts the zip contents into it.
|
unzip(String source, File outputDir) |
source: Base64-encoded zip string; outputDir: target directory |
void | Decodes the Base64 string using MIME decoder and extracts the zip contents to the output directory. |
unzipToTempDir(InputStream source, String prefix, String suffix) |
source: zip input stream; prefix/suffix: temp dir naming |
File -- path to the created temporary directory |
Creates a temporary directory and extracts the zip stream into it. |
unzip(InputStream source, File outputDir) |
source: zip input stream; outputDir: target directory |
void | Extracts zip entries from the stream to the output directory. Creates directories as needed and preserves last modified times. |
unzipFile(File output, InputStream zipStream, String name) |
output: target directory; zipStream: stream positioned at entry data; name: entry name |
void | Extracts a single zip entry. Includes zip slip protection: throws IOException if the resolved path escapes the target directory. Creates parent directories as needed.
|
Usage Examples
import org.openqa.selenium.io.Zip;
import java.io.File;
// Zip a directory to a Base64 string (for wire protocol transfer)
File extensionDir = new File("/path/to/extension");
String base64Zip = Zip.zip(extensionDir);
// Zip a single file
File fileToUpload = new File("/path/to/upload.txt");
String base64File = Zip.zip(fileToUpload);
// Unzip a Base64 string to a specific directory
File outputDir = new File("/path/to/output");
Zip.unzip(base64Zip, outputDir);
// Unzip to a temporary directory
File tempDir = Zip.unzipToTempDir(base64Zip, "selenium", "ext");
// Unzip from an InputStream
try (InputStream zipStream = getZipStream()) {
Zip.unzip(zipStream, outputDir);
}
Related Pages
- SeleniumHQ_Selenium_ExternalProcess -- Process management utility in a related infrastructure package
- SeleniumHQ_Selenium_ExecutableFinder -- Executable locator utility in a related infrastructure package