HomeSketchwareHow to zip files in Sketchware

How to zip files in Sketchware

1) Create a new Sketchware project.
2) Add two Buttons with id button1 and button2 and a EditText with id edittext1
Then change the text of button1 and button2 to Zip files and folders and Unzip files .
Then add a file picker component and three string variables source, destination and path.
Then Create a more block zip with two strings source and destination.
And add another more block Unzip with one string path.

3) In button1 (To Zip folder) on click event do as below:-

4) In button2 (To Unzip File) on click event do as below:-

5) In onfilepicked event do as below :-

6) In more more zip paste the below code:-
try { java.util.zip.ZipOutputStream os = new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(_destination)); zip(os, _source, null); os.close(); } catch(java.io.IOException e) {} } private void zip(java.util.zip.ZipOutputStream os, String filePath, String name) throws java.io.IOException { java.io.File file = new java.io.File(filePath); java.util.zip.ZipEntry entry = new java.util.zip.ZipEntry((name != null ? name + java.io.File.separator : “”) + file.getName() + (file.isDirectory() ? java.io.File.separator : “”)); os.putNextEntry(entry); if(file.isFile()) { java.io.InputStream is = new java.io.FileInputStream(file); byte[] buff = new byte[size]; int len = is.read(buff); os.write(buff, 0, len); return; } java.io.File[] fileArr = file.listFiles(); for(java.io.File subFile : fileArr) { zip(os, subFile.getAbsolutePath(), entry.getName()); }

 7) In more block Unzip paste the below code:-
ZipMan thanks = new ZipMan(); thanks.Unzip(_path); } public static class ZipMan { public static void main(String[] args) throws Exception{ String sourceFolderName = “/sdcard/aan”; String outputFileName = “/sdcard/aan.zip”; java.io.FileOutputStream fos = new java.io.FileOutputStream(outputFileName); java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos); zos.setLevel(9); System.out.println(“Begin to compress folder : ” + sourceFolderName + ” to ” + outputFileName); _Zip(zos, sourceFolderName, sourceFolderName); zos.close(); System.out.println(“Program ended successfully!”); } private static void _Zip(java.util.zip.ZipOutputStream zos,String folderName,String baseFolderName)throws Exception{ java.io.File f = new java.io.File(folderName); if(f.exists()){ if(f.isDirectory()){ if(!folderName.equalsIgnoreCase(baseFolderName)){ String entryName = folderName.substring(baseFolderName.length()+1,folderName.length()) + java.io.File.separatorChar; System.out.println(“Adding folder entry ” + entryName); java.util.zip.ZipEntry ze= new java.util.zip.ZipEntry(entryName); zos.putNextEntry(ze); } java.io.File f2[] = f.listFiles(); for(int i=0;i<f2.length;i++){ _Zip(zos,f2[i].getAbsolutePath(),baseFolderName); } }else{ String entryName = folderName.substring(baseFolderName.length()+1,folderName.length()); System.out.print(“Adding file entry ” + entryName + “…”); java.util.zip.ZipEntry ze= new java.util.zip.ZipEntry(entryName); zos.putNextEntry(ze); java.io.FileInputStream in = new java.io.FileInputStream(folderName); int len; byte buffer[] = new byte[1024]; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); System.out.println(“OK!”); } }else{ System.out.println(“File or directory not found ” + folderName); } } private static void Unzip(String strZipFile) { try { java.io.File fSourceZip = new java.io.File(strZipFile); String zipPath = strZipFile.substring(0, strZipFile.length()-4); java.io.File temp = new java.io.File(zipPath); temp.mkdir(); System.out.println(zipPath + ” created”); java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(fSourceZip); java.util.Enumeration e = zipFile.entries(); while(e.hasMoreElements()) { java.util.zip.ZipEntry entry = (java.util.zip.ZipEntry)e.nextElement(); java.io.File destinationFilePath = new java.io.File(zipPath,entry.getName()); destinationFilePath.getParentFile().mkdirs(); if(entry.isDirectory()) { continue; } else { System.out.println(“Extracting ” + destinationFilePath); java.io.BufferedInputStream bis = new java.io.BufferedInputStream(zipFile.getInputStream(entry)); int b; byte buffer[] = new byte[1024]; java.io.FileOutputStream fos = new java.io.FileOutputStream(destinationFilePath); java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(fos,1024); while ((b = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, b); } //flush the output stream and close it. bos.flush(); bos.close(); //close the input stream. bis.close(); } } } catch(java.io.IOException ioe) { System.out.println(“IOError :” + ioe); } } } {

 7)That’s all. Watch the below video:-


Leave a Reply