Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 7 May 2022

UnZip files using Java

        import java.io.BufferedOutputStream;

        import java.io.File;

        import java.io.FileInputStream;

        import java.io.FileNotFoundException;

        import java.io.FileOutputStream;

        import java.io.IOException;

        import java.util.zip.GZIPInputStream;

        import java.util.zip.GZIPOutputStream;

        import java.util.zip.ZipEntry;

        import java.util.zip.ZipInputStream;

        import java.util.zip.ZipOutputStream;




       public void unZip(String inputFile, String outputFilePath) {

try {

File destDir = new File(outputFilePath);

byte[] buffer = new byte[1024];

ZipInputStream zis = new ZipInputStream(new FileInputStream(inputFile));

ZipEntry zipEntry = zis.getNextEntry();

while (zipEntry != null) {

File newFile = newFile(destDir, zipEntry);

if (zipEntry.isDirectory()) {

if (!newFile.isDirectory() && !newFile.mkdirs()) {

throw new IOException("Failed to create directory " + newFile);

}

} else {

// fix for Windows-created archives

File parent = newFile.getParentFile();

if (!parent.isDirectory() && !parent.mkdirs()) {

throw new IOException("Failed to create directory " + parent);

}


// write file content

FileOutputStream fos = new FileOutputStream(newFile);

int len;

while ((len = zis.read(buffer)) > 0) {

fos.write(buffer, 0, len);

}

fos.close();

}

zipEntry = zis.getNextEntry();

}

zis.closeEntry();

zis.close();

} catch (Exception e) {

System.out.println(e.getMessage());

}

}


        public static File newFile(File destinationDir, ZipEntry zipEntry) throws             IOException {

File destFile = new File(destinationDir, zipEntry.getName());


String destDirPath = destinationDir.getCanonicalPath();

String destFilePath = destFile.getCanonicalPath();


if (!destFilePath.startsWith(destDirPath + File.separator)) {

throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());

}


return destFile;

}

Gzip files in Java

 

        import java.io.BufferedOutputStream;

        import java.io.File;

        import java.io.FileInputStream;

        import java.io.FileNotFoundException;

        import java.io.FileOutputStream;

        import java.io.IOException;

        import java.util.zip.GZIPInputStream;

        import java.util.zip.GZIPOutputStream;

        import java.util.zip.ZipEntry;

        import java.util.zip.ZipInputStream;

        import java.util.zip.ZipOutputStream;



        public void gZipFile(String inputFile, String outputFile) {

try {

FileInputStream fis = new FileInputStream(inputFile);

FileOutputStream fos = new FileOutputStream(outputFile);

GZIPOutputStream gzipOS = new GZIPOutputStream(fos);

byte[] buffer = new byte[1024];

int len;

while ((len = fis.read(buffer)) != -1) {

gzipOS.write(buffer, 0, len);

}

// close resources

gzipOS.close();

fos.close();

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

Zip files using Java

 

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.GZIPInputStream;

import java.util.zip.GZIPOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;


public void zipFiles(String[] files, String outputFile) {

FileOutputStream fos = null;

ZipOutputStream zipOut = null;

FileInputStream fis = null;

try {

    fos = new FileOutputStream(outputFile);

    zipOut = new ZipOutputStream(new BufferedOutputStream(fos));

    for (String filePath : files) {

File input = new File(filePath);

fis = new FileInputStream(input);

ZipEntry ze = new ZipEntry(input.getName());

// System.out.println("Zipping the file: " + input.getName());

zipOut.putNextEntry(ze);

byte[] tmp = new byte[4 * 1024];

int size = 0;

while ((size = fis.read(tmp)) != -1) {

zipOut.write(tmp, 0, size);

}

zipOut.flush();

fis.close();

}

zipOut.close();

System.out.println("Done... Zipped the files...");

                catch (FileNotFoundException e) {

                catch (IOException e) {

                finally {

    try {

if (fos != null)

fos.close();

                    catch (Exception ex) {


}

}

}

Tuesday, 3 May 2022

Check whether given strings are anagram with each other

Problem statement: We have two strings  "abc" & "bac" and verify whether both string have same alphabets in any order


Sample input1:

"abc" & "bac"

Output1: 

Anagram


Sample input2:

"abc" & "aba"

Output2: 

Not Anagram

public void checkAnagram() {

  System.out.println("checkAnagram:");

  System.out.println("-------------");

  String input1="aba",input2="baa";

  Map<Character, Integer> map1=new HashMap<Character, Integer>();

  Map<Character, Integer> map2=new HashMap<Character, Integer>();

  if(input1.length()==input2.length()) {

  for(int i=0;i<input1.length();i++) {

if(!map1.containsKey(input1.charAt(i))) {

map1.put(input1.charAt(i), 1);

}

else {

map1.put(input1.charAt(i),map1.get(input1.charAt(i))+1);

}

if(!map2.containsKey(input2.charAt(i))) {

map2.put(input2.charAt(i), 1);

}

else {

map2.put(input2.charAt(i), map2.get(input2.charAt(i))+1);

}

  }

  if(map1.equals(map2)) {

System.out.println("Anagram");

  }

  else {

  System.out.println("Not Anagram");

  }

 }

  else {

  System.out.println("Not Anagram");

  }

}


Output: 

Anagram

Monday, 2 May 2022

Find the next greater number

 Find the next greater number:


        public void nextGreaterNumber() {

int[] numbers = { 65, 40, 50, 7, 6, 51, 16 };

int next = 0;

boolean flag = false;

for (int i = 0; i < numbers.length; i++) {

next = -1;

for (int j = i + 1; j < numbers.length; j++) {

if (numbers[i] < numbers[j]) {

next = numbers[j];

break;

}

}

System.out.println(numbers[i] + "-> " + next);

}

}


O/P: 

65-> -1

40-> 50

50-> 51

7-> 51

6-> 51

51-> -1

16-> -1

Remove duplicate in the String

 Remove duplicate in the String 


        public void removeDuplicateInString() {

String input="Helle";

String output="";

for(char ch:input.toCharArray()) {

if(!output.contains(String.valueOf(ch))) {

output+=ch;

}

}

System.out.println(output);

}


o/p: Hele

Switch case in Java

 Problem statement: Return the capital of a state based on input state          public String getCapital(String state){               switch...