Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
1f2f0b30
Commit
1f2f0b30
authored
Sep 22, 2015
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mem zip file refactor and test
parent
1abf85ed
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
40 deletions
+119
-40
memzipfile.go
memzipfile.go
+53
-40
memzipfile_test.go
memzipfile_test.go
+66
-0
No files found.
memzipfile.go
View file @
1f2f0b30
...
...
@@ -15,6 +15,7 @@
package
beego
import
(
"bufio"
"bytes"
"compress/flate"
"compress/gzip"
...
...
@@ -49,57 +50,26 @@ func openMemZipFile(path string, zip string) (*memFile, error) {
modTime
:=
osFileInfo
.
ModTime
()
fileSize
:=
osFileInfo
.
Size
()
mapKey
:=
zip
+
":"
+
path
lock
.
RLock
()
cfi
,
ok
:=
menFileInfoMap
[
zip
+
":"
+
path
]
cfi
,
ok
:=
menFileInfoMap
[
mapKey
]
lock
.
RUnlock
()
if
!
(
ok
&&
cfi
.
ModTime
()
==
modTime
&&
cfi
.
fileSize
==
fileSize
)
{
var
content
[]
byte
if
zip
==
"gzip"
{
var
zipBuf
bytes
.
Buffer
gzipWriter
,
e
:=
gzip
.
NewWriterLevel
(
&
zipBuf
,
gzip
.
BestCompression
)
if
e
!=
nil
{
return
nil
,
e
}
_
,
e
=
io
.
Copy
(
gzipWriter
,
osFile
)
gzipWriter
.
Close
()
if
e
!=
nil
{
return
nil
,
e
}
content
,
e
=
ioutil
.
ReadAll
(
&
zipBuf
)
if
e
!=
nil
{
return
nil
,
e
}
}
else
if
zip
==
"deflate"
{
var
zipBuf
bytes
.
Buffer
deflateWriter
,
e
:=
flate
.
NewWriter
(
&
zipBuf
,
flate
.
BestCompression
)
if
e
!=
nil
{
return
nil
,
e
}
_
,
e
=
io
.
Copy
(
deflateWriter
,
osFile
)
deflateWriter
.
Close
()
if
e
!=
nil
{
return
nil
,
e
}
content
,
e
=
ioutil
.
ReadAll
(
&
zipBuf
)
if
e
!=
nil
{
return
nil
,
e
}
}
else
{
content
,
e
=
ioutil
.
ReadAll
(
osFile
)
lock
.
Lock
()
defer
lock
.
Unlock
()
if
cfi
,
ok
=
menFileInfoMap
[
mapKey
];
!
ok
{
cfi
,
e
=
newMenFileInfo
(
osFile
,
osFileInfo
,
zip
)
if
e
!=
nil
{
return
nil
,
e
}
menFileInfoMap
[
mapKey
]
=
cfi
}
cfi
=
&
memFileInfo
{
osFileInfo
,
modTime
,
content
,
int64
(
len
(
content
)),
fileSize
}
lock
.
Lock
()
defer
lock
.
Unlock
()
menFileInfoMap
[
zip
+
":"
+
path
]
=
cfi
}
return
&
memFile
{
fi
:
cfi
,
offset
:
0
},
nil
}
//
M
emFileInfo contains a compressed file bytes and file information.
//
m
emFileInfo contains a compressed file bytes and file information.
// it implements os.FileInfo interface.
type
memFileInfo
struct
{
os
.
FileInfo
...
...
@@ -109,6 +79,49 @@ type memFileInfo struct {
fileSize
int64
}
// newMenFileInfo return a memFileInfo from file by zip type
func
newMenFileInfo
(
file
*
os
.
File
,
fileInfo
os
.
FileInfo
,
zip
string
)
(
*
memFileInfo
,
error
)
{
var
content
[]
byte
var
zipBuf
bytes
.
Buffer
var
fileWriter
io
.
Writer
var
err
error
switch
zip
{
case
"gzip"
:
fileWriter
,
err
=
gzip
.
NewWriterLevel
(
&
zipBuf
,
gzip
.
BestCompression
)
case
"deflate"
:
fileWriter
,
err
=
flate
.
NewWriter
(
&
zipBuf
,
flate
.
BestCompression
)
default
:
fileWriter
=
bufio
.
NewWriter
(
&
zipBuf
)
}
if
err
!=
nil
{
return
nil
,
err
}
_
,
err
=
io
.
Copy
(
fileWriter
,
file
)
if
err
!=
nil
{
return
nil
,
err
}
switch
fileWriter
.
(
type
)
{
case
io
.
WriteCloser
:
fileWriter
.
(
io
.
WriteCloser
)
.
Close
()
}
content
,
err
=
ioutil
.
ReadAll
(
&
zipBuf
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
memFileInfo
{
FileInfo
:
fileInfo
,
modTime
:
fileInfo
.
ModTime
(),
content
:
content
,
contentSize
:
int64
(
len
(
content
)),
fileSize
:
fileInfo
.
Size
(),
},
nil
}
// Name returns the compressed filename.
func
(
fi
*
memFileInfo
)
Name
()
string
{
return
fi
.
Name
()
...
...
memzipfile_test.go
0 → 100644
View file @
1f2f0b30
package
beego
import
(
"bytes"
"compress/flate"
"compress/gzip"
"io"
"io/ioutil"
"os"
"testing"
)
const
licenseFile
=
"./LICENSE"
func
TestOpenMemZipFile_1
(
t
*
testing
.
T
)
{
mf
,
err
:=
openMemZipFile
(
licenseFile
,
""
)
if
err
!=
nil
{
t
.
Fail
()
}
file
,
_
:=
os
.
Open
(
licenseFile
)
content
,
_
:=
ioutil
.
ReadAll
(
file
)
assetMenFileAndContent
(
mf
,
content
,
t
)
}
func
assetMenFileAndContent
(
mf
*
memFile
,
content
[]
byte
,
t
*
testing
.
T
)
{
if
mf
.
fi
.
contentSize
!=
int64
(
len
(
content
))
{
t
.
Log
(
"content size not same"
)
t
.
Fail
()
}
for
i
,
v
:=
range
content
{
if
v
!=
mf
.
fi
.
content
[
i
]
{
t
.
Log
(
"content not same"
)
t
.
Fail
()
}
}
if
len
(
menFileInfoMap
)
==
0
{
t
.
Log
(
"men map is empty"
)
t
.
Fail
()
}
}
func
TestOpenMemZipFile_2
(
t
*
testing
.
T
)
{
mf
,
err
:=
openMemZipFile
(
licenseFile
,
"gzip"
)
if
err
!=
nil
{
t
.
Fail
()
}
file
,
_
:=
os
.
Open
(
licenseFile
)
var
zipBuf
bytes
.
Buffer
fileWriter
,
_
:=
gzip
.
NewWriterLevel
(
&
zipBuf
,
gzip
.
BestCompression
)
io
.
Copy
(
fileWriter
,
file
)
fileWriter
.
Close
()
content
,
_
:=
ioutil
.
ReadAll
(
&
zipBuf
)
assetMenFileAndContent
(
mf
,
content
,
t
)
}
func
TestOpenMemZipFile_3
(
t
*
testing
.
T
)
{
mf
,
err
:=
openMemZipFile
(
licenseFile
,
"deflate"
)
if
err
!=
nil
{
t
.
Fail
()
}
file
,
_
:=
os
.
Open
(
licenseFile
)
var
zipBuf
bytes
.
Buffer
fileWriter
,
_
:=
flate
.
NewWriter
(
&
zipBuf
,
flate
.
BestCompression
)
io
.
Copy
(
fileWriter
,
file
)
fileWriter
.
Close
()
content
,
_
:=
ioutil
.
ReadAll
(
&
zipBuf
)
assetMenFileAndContent
(
mf
,
content
,
t
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment