/*
* Copyright (C) 2012 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.filemanager.commands.shell;
import com.cyanogenmod.filemanager.commands.AsyncResultListener;
import com.cyanogenmod.filemanager.commands.SIGNAL;
import com.cyanogenmod.filemanager.commands.UncompressExecutable;
import com.cyanogenmod.filemanager.console.CommandNotFoundException;
import com.cyanogenmod.filemanager.console.ExecutionException;
import com.cyanogenmod.filemanager.console.InsufficientPermissionsException;
import com.cyanogenmod.filemanager.preferences.UncompressionMode;
import com.cyanogenmod.filemanager.util.FileHelper;
import java.io.File;
/**
* A class for uncompress file system objects.
*
* {@link "http://unixhelp.ed.ac.uk/CGI/man-cgi?tar"}
* {@link "http://unixhelp.ed.ac.uk/CGI/man-cgi?unzip"}
* {@link "http://unixhelp.ed.ac.uk/CGI/man-cgi?gunzip"}
* {@link "http://unixhelp.ed.ac.uk/CGI/man-cgi?bunzip2"}
* {@link "http://unixhelp.ed.ac.uk/CGI/man-cgi?xz"}
*/
public class UncompressCommand extends AsyncResultProgram implements UncompressExecutable {
/**
* An enumeration of implemented uncompression modes.
*/
private enum Mode {
/**
* Uncompress using Tar algorithm
*/
A_UNTAR(UNTAR_ID, "", UncompressionMode.A_UNTAR), //$NON-NLS-1$
/**
* Uncompress using Tar algorithm
*/
A_UNZIP(UNZIP_ID, "", UncompressionMode.A_UNZIP), //$NON-NLS-1$
/**
* Uncompress using Gzip algorithm
*/
AC_GUNZIP(UNTAR_ID, "z", UncompressionMode.AC_GUNZIP), //$NON-NLS-1$
/**
* Uncompress using Gzip algorithm
*/
AC_GUNZIP2(UNTAR_ID, "z", UncompressionMode.AC_GUNZIP2), //$NON-NLS-1$
/**
* Uncompress using Bzip algorithm
*/
AC_BUNZIP(UNTAR_ID, "j", UncompressionMode.AC_BUNZIP), //$NON-NLS-1$
/**
* Uncompress using Lzma algorithm
*/
AC_UNLZMA(UNTAR_ID, "a", UncompressionMode.AC_UNLZMA), //$NON-NLS-1$
/**
* Uncompress using Gzip algorithm
*/
C_GUNZIP(GUNZIP_ID, "", UncompressionMode.C_GUNZIP), //$NON-NLS-1$
/**
* Uncompress using Bzip algorithm
*/
C_BUNZIP(BUNZIP_ID, "", UncompressionMode.C_BUNZIP), //$NON-NLS-1$
/**
* Uncompress using Lzma algorithm
*/
C_UNLZMA(UNLZMA_ID, "", UncompressionMode.C_UNLZMA), //$NON-NLS-1$
/**
* Uncompress using Unix compress algorithm
*/
C_UNCOMPRESS(UNCOMPRESS_ID, "", UncompressionMode.C_UNCOMPRESS), //$NON-NLS-1$
/**
* Uncompress using Unix compress algorithm
*/
C_UNXZ(UNXZ_ID, "", UncompressionMode.C_UNXZ), //$NON-NLS-1$
/**
* Uncompress using Rar algorithm
*/
A_UNRAR(UNRAR_ID, "", UncompressionMode.C_UNRAR); //$NON-NLS-1$
final String mId;
final String mFlag;
UncompressionMode mMode;
/**
* Constructor of Mode
*
* @param id The command identifier
* @param flag The tar compression flag
* @param mode The uncompressed mode
*/
private Mode(String id, String flag, UncompressionMode mode) {
this.mId = id;
this.mFlag = flag;
this.mMode = mode;
}
}
private static final String UNTAR_ID = "untar"; //$NON-NLS-1$
private static final String UNZIP_ID = "unzip"; //$NON-NLS-1$
private static final String GUNZIP_ID = "gunzip"; //$NON-NLS-1$
private static final String BUNZIP_ID = "bunzip"; //$NON-NLS-1$
private static final String UNLZMA_ID = "unlzma"; //$NON-NLS-1$
private static final String UNCOMPRESS_ID = "uncompress"; //$NON-NLS-1$
private static final String UNXZ_ID = "unxz"; //$NON-NLS-1$
private static final String UNRAR_ID = "unrar"; //$NON-NLS-1$
private Boolean mResult;
private String mPartial;
private final String mOutFile;
private final Mode mMode;
/**
* Constructor of UncompressCommand.
*
*
null if no mode found
*/
private static Mode getMode(String src) {
String extension = FileHelper.getExtension(src);
Mode[] modes = Mode.values();
int cc = modes.length;
for (int i = 0; i < cc; i++) {
Mode mode = modes[i];
if (mode.mMode.mExtension.compareToIgnoreCase(extension) == 0) {
return mode;
}
}
return null;
}
}