99} from 'react-native-custom-tabs'
1010import AwesomeButtonRick from 'react-native-really-awesome-button/src/themes/rick'
1111import { DocumentPicker } from 'react-native-document-picker'
12- import { copyFile , DocumentDirectoryPath } from 'react-native-fs'
13- import { subscribe , unzipWithPassword , isPasswordProtected } from 'react-native-zip-archive'
12+ import { copyFile , DocumentDirectoryPath , unlink } from 'react-native-fs'
13+ import { subscribe , unzip , unzipWithPassword , isPasswordProtected } from 'react-native-zip-archive'
1414
1515export default class App extends Component {
1616 constructor ( props ) {
@@ -22,10 +22,10 @@ export default class App extends Component {
2222 }
2323
2424 /**
25- * Dropbox to download a sample password protected html file
25+ * box to download a sample password protected html file
2626 */
27- openLink ( ) {
28- let url = 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts'
27+ openLink ( isPasswordProtected ) {
28+ let url = isPasswordProtected ? 'https://app.box.com/s/2szqk4fzsq7brrbcnuk6z2tdl6jq2rts' : 'https://app.box.com/s/ndkn0exa9zmuh9ki7qpjgakvbkrn98q7 '
2929 CustomTabs . openURL ( url , {
3030 toolbarColor : '#607D8B' ,
3131 enableUrlBarHiding : true ,
@@ -40,8 +40,11 @@ export default class App extends Component {
4040 */
4141 browseFiles ( ) {
4242 DocumentPicker . show ( {
43- filetype : [ ( Platform . OS === 'android' ) ? " */*" : " public.data" ]
43+ filetype : [ ( Platform . OS === 'android' ) ? ' */*' : ' public.data' ]
4444 } , ( err , response ) => {
45+ if ( err ) {
46+ console . error ( err )
47+ }
4548 var fileDetails = {
4649 uri : response . uri ,
4750 name : response . fileName ,
@@ -80,23 +83,25 @@ export default class App extends Component {
8083 var filename = fileDetails . name
8184 var filePath = DocumentDirectoryPath + '/' + filename
8285 var unzipPath = DocumentDirectoryPath
83- copyFile ( fileDetails . uri , filePath ) . catch ( ( err ) => {
86+ unlink ( filePath ) . catch ( err => {
8487 console . log ( err )
8588 return Promise . resolve ( )
89+ } ) . then ( ( ) => {
90+ return copyFile ( fileDetails . uri , filePath )
8691 } ) . then ( ( ) => {
8792 return isPasswordProtected ( filePath )
8893 } ) . then ( ( isEncrypted ) => {
8994 if ( isEncrypted ) {
9095 return unzipWithPassword ( filePath , unzipPath , password )
9196 } else {
92- throw 'Not password protected!'
97+ return unzip ( filePath , unzipPath )
9398 }
9499 } ) . then ( ( response ) => {
95100 console . log ( 'Successfully unzipped files' )
96101 console . log ( response )
97102 this . setState ( {
98103 ...this . state ,
99- uri : `file://${ unzipPath } /static_password /index.html`
104+ uri : `file://${ filePath . split ( '.' ) . slice ( 0 , - 1 ) . join ( '.' ) } /index.html`
100105 } )
101106 } ) . catch ( ( err ) => {
102107 console . log ( err )
@@ -124,10 +129,21 @@ export default class App extends Component {
124129 < AwesomeButtonRick
125130 borderRadius = { 8 }
126131 width = { 300 }
127- textSize = { 22 }
132+ textSize = { 14 }
133+ type = "primary"
134+ onPress = { ( ) => this . openLink ( true ) } >
135+ Download Sample Zip file with password
136+ </ AwesomeButtonRick >
137+
138+ < View style = { styles . lineStyle } />
139+
140+ < AwesomeButtonRick
141+ borderRadius = { 8 }
142+ width = { 300 }
143+ textSize = { 14 }
128144 type = "primary"
129- onPress = { ( ) => this . openLink ( ) } >
130- Download Sample Zip file
145+ onPress = { ( ) => this . openLink ( false ) } >
146+ Download Sample Zip file without password
131147 </ AwesomeButtonRick >
132148
133149 < View style = { styles . lineStyle } />
0 commit comments