PHPZip usage example for PHP File compression

Source: Internet
Author: User
Tags bit set crc32

PHPZip usage example for PHP File compression

This document describes how to use the PHP zip class for PHP File compression. Share it with you for your reference. The details are as follows:

?

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

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

<? Php

//

// PHPZip v1.2 by Sext (sext@neud.net)

//

// Makes zip archive

//

// Based on "Zip file creation class", uses zLib

//

//

Class PHPZip

{

Function Zip ($ dir, $ zipfilename)

{

If (@ function_exists ('gzcompress '))

{

$ Curdir = getcwd ();

If (is_array ($ dir ))

{

$ Filelist = $ dir;

}

Else

{

$ Filelist = $ this-> GetFileList ($ dir );

}

If ((! Empty ($ dir ))&&(! Is_array ($ dir) & (file_exists ($ dir) chdir ($ dir );

Else chdir ($ curdir );

If (count ($ filelist)> 0)

{

Foreach ($ filelist as $ filename)

{

If (is_file ($ filename ))

{

$ Fd = fopen ($ filename, "r ");

$ Content = fread ($ fd, filesize ($ filename ));

Fclose ($ fd );

If (is_array ($ dir) $ filename = basename ($ filename );

$ This-> addFile ($ content, $ filename );

}

}

$ Out = $ this-> file ();

Chdir ($ curdir );

$ Fp = fopen ($ zipfilename, "w ");

Fwrite ($ fp, $ out, strlen ($ out ));

Fclose ($ fp );

}

Return 1;

}

Else return 0;

}

Function GetFileList ($ dir)

{

If (file_exists ($ dir ))

{

$ Args = func_get_args ();

$ Pref = $ args [1];

$ Dh = opendir ($ dir );

While ($ files = readdir ($ dh ))

{

If ($ files! = ".") & ($ Files! = ".."))

{

If (is_dir ($ dir. $ files ))

{

$ Curdir = getcwd ();

Chdir ($ dir. $ files );

$ File = array_merge ($ file, $ this-> GetFileList ("", "$ pref $ files /"));

Chdir ($ curdir );

}

Else $ file [] = $ pref. $ files;

}

}

Closedir ($ dh );

}

Return $ file;

}

Var $ datasec = array ();

Var $ ctrl_dir = array ();

Var $ eof_ctrl_dir = "x50x4bx05x06x00x00x00x00 ";

Var $ old_offset = 0;

/**

* Converts an Unix timestamp to a four byte DOS date and time format (date

* In high two bytes, time in low two bytes allowing magn1_comparison ).

*

* @ Param integer the current Unix timestamp

*

* @ Return integer the current date in a four byte DOS format

*

* @ Access private

*/

Function unix2DosTime ($ unixtime = 0 ){

$ Timearray = ($ unixtime = 0 )? Getdate (): getdate ($ unixtime );

If ($ timearray ['Year'] <1980 ){

$ Timearray ['Year'] = 1980;

$ Timearray ['mon'] = 1;

$ Timearray ['mday'] = 1;

$ Timearray ['urs'] = 0;

$ Timearray ['minutes '] = 0;

$ Timearray ['seconds'] = 0;

} // End if

Return ($ timearray ['Year']-1980) <25) | ($ timearray ['mon'] <21) | ($ timearray ['mday'] <16) |

($ Timearray ['urs'] <11) | ($ timearray ['minutes '] <5) | ($ timearray ['seconds']> 1 );

} // End of the 'unix2dostime () 'method

/**

* Adds "file" to archive

*

* @ Param string file contents

* @ Param string name of the file in the archive (may contains the path)

* @ Param integer the current timestamp

*

* @ Access public

*/

Function addFile ($ data, $ name, $ time = 0)

{

$ Name = str_replace ('', '/', $ name );

 

$ Dtime = dechex ($ this-> unix2DosTime ($ time ));

$ Hexdtime = 'x'. $ dtime [6]. $ dtime [7]

. 'X'. $ dtime [4]. $ dtime [5]

. 'X'. $ dtime [2]. $ dtime [3]

. 'X'. $ dtime [0]. $ dtime [1];

Eval ('$ hexdtime = "'. $ hexdtime .'";');

$ Fr = "x50x4bx03x04 ";

$ Fr. = "x14x00"; // ver needed to extract

$ Fr. = "x00x00"; // gen purpose bit flag

$ Fr. = "x08x00"; // compression method

$ Fr. = $ hexdtime; // last mod time and date

 

// "Local file header" segment

$ Unc_len = strlen ($ data );

$ Crc = crc32 ($ data );

$ Zdata = gzcompress ($ data );

$ C_len = strlen ($ zdata );

$ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the crc bug

$ Fr. = pack ('V', $ crc); // crc32

$ Fr. = pack ('V', $ c_len); // compressed filesize

$ Fr. = pack ('V', $ unc_len); // uncompressed filesize

$ Fr. = pack ('V', strlen ($ name); // length of filename

$ Fr. = pack ('V', 0); // extra field length

$ Fr. = $ name;

// "File data" segment

$ Fr. = $ zdata;

// "Data descriptor" segment (optional but necessary if archive is not

// Served as file)

$ Fr. = pack ('V', $ crc); // crc32

$ Fr. = pack ('V', $ c_len); // compressed filesize

$ Fr. = pack ('V', $ unc_len); // uncompressed filesize

// Add this entry to array

$ This-> datasec [] = $ fr;

$ New_offset = strlen (implode ('', $ this-> datasec ));

// Now add to central directory record

$ Cdrec = "x50x4bx01x02 ";

$ Cdrec. = "x00x00"; // version made

$ Cdrec. = "x14x00"; // version needed to extract

$ Cdrec. = "x00x00"; // gen purpose bit flag

$ Cdrec. = "x08x00"; // compression method

$ Cdrec. = $ hexdtime; // last mod time & date

$ Cdrec. = pack ('V', $ crc); // crc32

$ Cdrec. = pack ('V', $ c_len); // compressed filesize

$ Cdrec. = pack ('V', $ unc_len); // uncompressed filesize

$ Cdrec. = pack ('V', strlen ($ name); // length of filename

$ Cdrec. = pack ('V', 0); // extra field length

$ Cdrec. = pack ('V', 0); // file comment length

$ Cdrec. = pack ('V', 0); // disk number start

$ Cdrec. = pack ('V', 0); // internal file attributes

$ Cdrec. = pack ('V', 32); // external file attributes-'archive' bit set

$ Cdrec. = pack ('V', $ this-> old_offset); // relative offset of local header

$ This-> old_offset = $ new_offset;

$ Cdrec. = $ name;

// Optional extra field, file comment goes here

// Save to central directory

$ This-> ctrl_dir [] = $ cdrec;

} // End of the 'addfile () 'method

/**

* Dumps out file

*

* @ Return string the zipped file

*

* @ Access public

*/

Function file ()

{

$ Data = implode ('', $ this-> datasec );

$ Ctrldir = implode ('', $ this-> ctrl_dir );

 

Return

$ Data.

$ Ctrldir.

$ This-> eof_ctrl_dir.

Pack ('V', sizeof ($ this-> ctrl_dir). // total # of entries "on this disk"

Pack ('V', sizeof ($ this-> ctrl_dir). // total # of entries overall

Pack ('V', strlen ($ ctrldir). // size of central dir

Pack ('V', strlen ($ data). // offset to start of central dir

"X00x00"; //. zip file comment length

} // End of the 'file () 'method

} // End of the 'phpzip' class

?>

Usage:

?

1

2

3

4

5

6

7

8

9

10

11

<? Php

$ Z = new PHPZip (); // create a zip class

// Method 1:

$ Z-> Zip ("", "out1.zip"); // Add all files under the current directory and subdirectory

// Method 2:

Using filespolicarray('1.txt', 'gb.txt ');

Using files000000000000'5.txt ';

$ Z-> Zip ($ files, "out2.zip"); // Add a file list

// Method 3:

$ Z-> Zip ("/usr/local/sext/", "out3.zip"); // Add a specified directory

?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.