Home php教程 PHP源码 使用 TbsZip 对 Zip 压缩文件进行操作

使用 TbsZip 对 Zip 压缩文件进行操作

May 25, 2016 pm 05:15 PM


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

CreateNew(); // start a new empty archive for adding files

// or

$zip->Open('an_existing_archive.zip'); // open an existing archive for reading and/or modifying

 

 

// --------------------------------------------------

// Reading information and data in the opened archive

// --------------------------------------------------

 

// check if a file is existing in the archive, the name must precise subfolders if any

$ok = $zip->FileExists('subfolder/help.html');

 

// count the files stored in the archive

$file_nbr = count($zip->CdFileLst);

 

// retrieve the content of an compressed file in the archive

$text1 = $zip->FileRead('readme.txt');

 

// retrieve the content of an compressed file in a subfolder of the archive

$text2 = $zip->FileRead('subfolder/readme.txt');

 

 

if ($ok) $zip->FileExists('subfolder/help.html');

 

// -----------------------------

// Modifying data in the archive

// -----------------------------

 

// add a file in the archive

$zip->FileAdd('newfile.txt', $data, TBSZIP_STRING); // add the file by giving the content

$zip->FileAdd('newpic1.png', './images/localpic1.png', TBSZIP_FILE);        // add the file by copying a local file

$zip->FileAdd('newpic2.png', './images/localpic2.png', TBSZIP_FILE, false); // add the uncompressed file by copying a local file

 

// delete an existing file in the archive

$zip->FileReplace('newfile.txt', $data, TBSZIP_STRING); // replace the file by giving the content

$zip->FileReplace('newpic1.png', './images/localpic1.png', TBSZIP_FILE);        // replace the file by copying a local file

$zip->FileReplace('newpic2.png', './images/localpic2.png', TBSZIP_FILE, false); // replace the uncompressed file by copying a local file

$zip->FileReplace('newpic3.png', false);                                        // delete the file in the archive

 

// cancel the last modification on the file (add/replace/delete)

$zip->FileCancelModif('newpic2.png');

 

// ----------------------

// Applying modifications

// ----------------------

 

$zip->Flush(TBSZIP_FILE, './save/new_archive.zip'); // apply modifications as a new local file

 

// apply modifications as an HTTP downloaded file

$zip->Flush(TBSZIP_DOWNLOAD, 'download.zip');

$zip->Flush(TBSZIP_DOWNLOAD, 'download.zip', 'application/zip'); // with a specific Content-Type

 

// apply modifications as a downloaded file with your customized HTTP headers

header("Content-type: application/force-download");

header("Content-Disposition: attachment; filename=download.zip");

header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");

$zip->Flush(TBSZIP_DOWNLOAD+TBSZIP_NOHEADER);

 

 

// -----------------

// Close the archive

// -----------------

 

$zip->Close(); // stop to work with the opened archive. Modifications are not applied to the opened archive, use Flush() to commit

Copy after login

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)