Archive: Creating an Archive File as Well as Various Other Kinds of Manipulation
This tutorial demonstrates how you can create, browse, and extract ZIP archives.
The Archive API is mandatory for both Tizen mobile and wearable profiles, which means that it is supported in all mobile and wearable devices. The Archive API is supported on all Tizen Emulators.
Warm-up
Become familiar with the Archive API basics by learning about:
-
Reading the Content of an Archive
Open an archive and check its content.
-
Creating an Archive
Create a new archive file and add files to it.
-
Extracting Files from an Archive
Extract all files from an archive, or extract a selected file.
Reading the Content of an Archive
Opening an archive and accessing a list of its members is a basic archive management skill:
-
To access the archive file, use the open() method of the ArchiveManager interface (in mobile and wearable applications). The provided callback function receives an ArchiveFile object (in mobile and wearable applications).
var myArchive = null; function openSuccess(arch) { console.log("ArchiveFile mode: " + arch.mode); myArchive = arch; } tizen.archive.open("downloads/archive.zip", "r", openSuccess);
-
Get the list of all files contained inside the archive using the getEntries() method of the ArchiveFile interface.
function listSuccess(members) { if (members.length === 0) { console.log("The archive is empty"); return; } console.log("Files in the archive:") for (var i=0; i<members.length; i++) { console.log(members[i].name); } } myArchive.getEntries(listSuccess);
-
After the work with the archive is finished, close the archive using the close() method of the ArchiveFile interface.
archive.close();
Creating an Archive
Creating an archive and adding files to it is a basic archive management skill:
-
To create the archive file, use the open() method of the ArchiveManager interface (in mobile and wearable applications) and set the mode as w:
tizen.archive.open("downloads/new_archive.zip", "w", createSuccess);
- Add a file to the archive using the add() method. The file can be specified using a virtual path:
function progressCallback(opId, val, name) { console.log("opId: " + opId + " with progress val: " + (val * 100).toFixed(0) + "%"); } function successCallback() { console.log("File added"); } function createSuccess(archive) { archive.add("downloads/file.txt", successCallback, null, progressCallback); }
Extracting Files from an Archive
Extracting a file from an archive is a basic archive management skill:
-
To access an archive file, use the open() method of the ArchiveManager interface (in mobile and wearable applications). The "r" mode is suitable for extracting from the archive.
tizen.archive.open("downloads/some_archive.zip", "r", openSuccess, openError);
- To extract files:
-
To extract all files from the archive, use the extractAll() method of the ArchiveFile interface (in mobile and wearable applications).
function progressCallback(opId, val, name) { console.log("extracting operation (: " + opId + ") is in progress (" + (val * 100).toFixed(1) + "%)"); } function openSuccess(archive) { archive.extractAll("music", null, null, progressCallback); }
-
To extract only a selected file from the archive, use the extract() method of the ArchiveFileEntry interface (in mobile and wearable applications).
First, get the archiveFileEntry object using the getEntryByName() or getEntries() method of the ArchiveFile interface.
function extractSuccessCallback() { console.log("File extracted"); } function getEntrySuccess(entry) { entry.extract("downloads/extract", extractSuccessCallback); } function openSuccess(archive) { archive.getEntryByName("my_file.txt", getEntrySuccess); }
-