I am using the following code to build a zip file on my external storage, the only problem is that the file can't be extracted on a Windows PC or be used for anything other than Android, I think I have narrowed the problem to a non-existent folder.
My question is what have I done wrong to cause a weird format of zip?
/**
EXAMPLE USAGE : zipFolder("/sdcard0/downloads", "/sdcard0/Update.zip")
**/
static public void zipFolder(String srcFolder, String destZipFile)
throws Exception
{
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
static String firstFolder ="";
static private void addFileToZip(String path, String srcFile,
ZipOutputStream zip) throws Exception
{
if (firstFolder == "")
{
firstFolder = getLastPathComponent(path);
}
File folder = new File(srcFile);
if (folder.isDirectory())
{
addFolderToZip(path, srcFile, zip);
}
else
{
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path.replace(firstFolder, "") + "/" + folder.getName()));
while ((len = in.read(buf)) > 0)
{
zip.write(buf, 0, len);
}
}
}
public static String getLastPathComponent(String filePath)
{
String[] segments = filePath.split("/");
if (segments.length == 0)
return "";
String lastPathComponent = segments[segments.length - 1];
return lastPathComponent;
}
static private void addFolderToZip(String path, String srcFolder,
ZipOutputStream zip) throws Exception
{
File folder = new File(srcFolder);
for (String fileName : folder.list())
{
if (path.equals(""))
{
addFileToZip(folder.getName().replace(firstFolder, ""), srcFolder + "/" + fileName, zip);
}
else
{
addFileToZip(path + "/" + folder.getName(), srcFolder + "/"
+ fileName, zip);
}
}
}
Related
I'm running into a problem using the commons compress library to create a tar.gz of a directory. I have a directory structure that is as follows.
parent/
child/
file1.raw
file2.raw
file3.raw
I hope the compressed structure is like this.
child/
file1.raw
file2.raw
file3.raw
Is there any way to remove the outermost layer during compression?
I've seen such examples, but I can't work properly,and can only handle fixed name structures
public static void main(String[] args) throws IOException {
String hallFilePath = "E:/" + "packs";
compress(Paths.get(hallFilePath).toString(), hallFilePath + ".zip");
}
public static void compress(String fromPath, String toPath) throws IOException {
File fromFile = new File(fromPath);
File toFile = new File(toPath);
if (!fromFile.exists()) {
throw new ServiceException(fromPath + "不存在!");
}
try (FileOutputStream outputStream = new FileOutputStream(toFile); CheckedOutputStream checkedOutputStream = new CheckedOutputStream(outputStream, new CRC32()); ZipOutputStream zipOutputStream = new ZipOutputStream(checkedOutputStream)) {
String baseDir = "";
compress(fromFile, zipOutputStream, baseDir);
}
}
private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
if (file.isDirectory()) {
compressDirectory(file, zipOut, baseDir);
} else {
if (baseDir.equals("packs" + File.separator)) {
baseDir = File.separator;
} else if (baseDir.equals("packs" + File.separator + "examineeInfo" + File.separator)) {
baseDir = "examineeInfo" + File.separator;
}
compressFile(file, zipOut, baseDir);
}
}
private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
if (!file.exists()) {
return;
}
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zipOut.putNextEntry(entry);
int count;
byte[] data = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zipOut.write(data, 0, count);
}
}
}
private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException {
File[] files = dir.listFiles();
if (files != null && ArrayUtils.isNotEmpty(files)) {
for (File file : files) {
compress(file, zipOut, baseDir + dir.getName() + File.separator);
}
}
}
I have a problem with springboot application that should work as a server for android application. I have audio file (.wav) that android application should receive from a server.
On local host it works very well, but when I make it on linux server there NullPointerException is appearing.
Here is my code of AudioController:
#RestController
#RequestMapping("/audiovideo")
public class AudioController {
public static final String AUDIO_PATH = "/root/audios/";
public static final int BYTE_RANGE = 128;
#GetMapping("/audios/{fileName}")
public Mono<ResponseEntity<byte[]>> streamAudio(#RequestHeader(value = "Range", required = false) String httpRangeList,
#PathVariable("fileName") String fileName) {
return Mono.just(getContent(AUDIO_PATH, fileName, httpRangeList, "audio"));
}
private ResponseEntity<byte[]> getContent(String location, String fileName, String range, String contentTypePrefix) {
long rangeStart = 0;
long rangeEnd;
byte[] data;
Long fileSize;
String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
try {
fileSize = Optional.ofNullable(fileName)
.map(file -> Paths.get(getFilePath(location), file))
.map(this::sizeFromFile)
.orElse(0L);
if (range == null) {
return ResponseEntity.status(HttpStatus.OK)
.header("Content-Type", contentTypePrefix+"/" + fileType)
.header("Content-Length", String.valueOf(fileSize))
.body(readByteRange(location, fileName, rangeStart, fileSize - 1));
}
String[] ranges = range.split("-");
rangeStart = Long.parseLong(ranges[0].substring(6));
if (ranges.length > 1) {
rangeEnd = Long.parseLong(ranges[1]);
} else {
rangeEnd = fileSize - 1;
}
if (fileSize < rangeEnd) {
rangeEnd = fileSize - 1;
}
data = readByteRange(location, fileName, rangeStart, rangeEnd);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
String contentLength = String.valueOf((rangeEnd - rangeStart) + 1);
return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
.header("Content-Type", contentTypePrefix + "/" + fileType)
.header("Accept-Ranges", "bytes")
.header("Content-Length", contentLength)
.header("Content-Range", "bytes" + " " + rangeStart + "-" + rangeEnd + "/" + fileSize)
.body(data);
}
public byte[] readByteRange(String location, String filename, long start, long end) throws IOException {
Path path = Paths.get(getFilePath(location), filename);
try (InputStream inputStream = (Files.newInputStream(path));
ByteArrayOutputStream bufferedOutputStream = new ByteArrayOutputStream()) {
byte[] data = new byte[BYTE_RANGE];
int nRead;
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
bufferedOutputStream.write(data, 0, nRead);
}
bufferedOutputStream.flush();
byte[] result = new byte[(int) (end - start) + 1];
System.arraycopy(bufferedOutputStream.toByteArray(), (int) start, result, 0, result.length);
return result;
}
}
private String getFilePath(String location) {
URL url = this.getClass().getResource(location);
return new File(url.getFile()).getAbsolutePath();
}
private Long sizeFromFile(Path path) {
try {
return Files.size(path);
} catch (IOException ex) {
e.printStackTrace();
}
return 0L;
}
}
File is located in right path (/root/audios/[FileName])
Is there any solution of this problem?
P.S. There is another controller for music info but it works perfectly and I don't know why is the audio controller isn't working correctly
Here it is:
#RequestMapping("/musicinfo/jsons")
#RestController
public class APIController {
#GetMapping(value = "/{filename}")
public ResponseEntity<StreamingResponseBody> streamInfo(#PathVariable String filename) throws FileNotFoundException{
File file = ResourceUtils.getFile("/root/musicinfo/" + filename);
StreamingResponseBody responseBody = outputStream -> {
Files.copy(file.toPath(), outputStream);
};
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(responseBody);
}
}
Thanks forehead for help!!!
I am running Mac OSX Mavericks. Right now I am creating a JAR file from a folder (org, the package). When I use this code from here:
public void run() throws IOException
{
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("/Users/username/Library/Application Support/VSE/temp/output.jar"), manifest);
add(new File("/Users/username/Library/Application Support/VSE/temp/org"), target);
target.close();
}
private void add(File source, JarOutputStream target) throws IOException
{
BufferedInputStream in = null;
try
{
if (source.isDirectory())
{
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true)
{
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
}
finally
{
if (in != null)
in.close();
}
}
When I extract the JAR file, There is a META-INF folder, but instead of having the org folder in the extracted jar, I have my Users folder copied into it (except because of it's size, its wasn't filled with all my stuff and my application crashed). I'm expecting this is because the code was written for a Windows system, and the differences with the filesystem (such as \ or /). How would I make the code include only the "org" directory, and not everything leading up to it?
Provided you use Java 7+ you may easily do this by using one of my packages in combination with the zip filesystem provider of the JDK to create it:
private static final Map<String, ?> ENV = Collections.singletonMap("create", "true");
public void run()
throws IOException
{
final Path zipPath = Paths.get("/Users/username/Library/Application Support/VSE/temp/output.jar");
final Path srcdir = Paths.get("/Users/username/Library/Application Support/VSE/temp/org");
final URI uri = URI.create("jar:" + zipPath.toUri());
Files.deleteIfExists(zipPath);
try (
final FileSystem zipfs = FileSystems.newFileSystem(uri, ENV);
) {
copyManifest(zipfs);
copyDirectory(srcdir, zipfs);
}
}
private void copyManifest(final FileSystem zipfs)
throws IOException
{
final Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
Files.createDirectory(zipfs.getPath("META-INF/");
try (
final OutputStream out = Files.newOutputStream(zipfs.getPath("META-INF/MANIFEST.MF"));
) {
manifest.write(out);
}
}
private void copyDirectory(final Path srcdir, final FileSystem zipfs)
{
final String lastName = srcdir.getFileName().toString();
final Path dstDir = zipfs.getPath(lastName);
Files.createDirectory(dstDir);
MoreFiles.copyRecursive(srcDir, dstDir, RecursionMode.FAIL_FAST);
}
I want to hash a file in Java by calling a file that ends with .raw. These are the codes I used:
FileSearch.java
public class FileSearch
{
private static final File file = null;
public static File findfile(File file) throws IOException
{
String drive = (new DetectDrive()).USBDetect();
Path start = FileSystems.getDefault().getPath(drive);
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
{
if (file.toString().endsWith(".raw"))
{
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
return file;
}
public static void main(String[] args) throws Exception
{
Hash hasher = new Hash();
FileSearch.findfile(file);
try
{
if (file.toString().endsWith("raw"))
{
hasher.hash(file);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
Hash.java
public class Hash
{
public void hash(File file) throws Exception
{
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1)
{
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++)
{
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digest(in hex format):: " + sb.toString());
}
}
The first code is used to find the file and perform hash by running the main method and the second code is the method for hashing the file (by MD5). However, when I run the it gives an ouput:
"name of raw file"
Exception in thread "main" java.lang.NullPointerException at FileSearch.main(FileSearch.java:33)
line 33 is the if (file.toString().endsWith("raw")) portion. Anyone knows how I can fix this?
You never initalize file with anything (Well, you initalize it with null)
private static final File file = null;
So when you call
if (file.toString().endsWith("raw"))
file can only be null.
What you probably want is just
file = FileSearch.findfile(file);
See:
What is a NullPointerException, and how do I fix it?
When I try to run my applet on the server, it never seems to go out the first step, that is, loading libraries, and when I try to run on localhost, works perfectly
CODE
private final static String DEFAULT_DOWNLOAD_PATH = "http://colorfulwolf.com/dev/cam/";
private final static String VERSION_ID = "1.0.0";
// note that this list is windows-specific, so this is not a generic
// solution that works on all OSes
private final static String[] LIBS = { "cv210.dll", "cvaux210.dll",
"cxcore210.dll", "cxts210.dll", "highgui210.dll", "ml210.dll" };
private final static String LIB_ARCHIVE = "opencv21.zip";
public void loadWebcam() {
loadingScreen.setMaxProgress(7);
loadingScreen.setProgress(1, "Loading Librarys..");
String tmpDir = System.getProperty("java.io.tmpdir");
File faPath = new File(tmpDir + File.separator + "WebcamApplet_"
+ VERSION_ID.replaceAll("\\.", "-"));
System.out.println(faPath);
System.setProperty("jna.library.path", faPath.getAbsolutePath());
String downloadPath = this.getParameter("dll_path");
if (downloadPath == null)
downloadPath = DEFAULT_DOWNLOAD_PATH;
try {
prepareLibraries(faPath, downloadPath);
} catch (Exception e) {
e.printStackTrace();
loadingScreen.setProgress(3, "Erro: " + e.getMessage());
return;
}
}
private void prepareLibraries(File localPath, String downloadPath)
throws Exception {
if (localPath.exists()) {
boolean libMissing = false;
for (String lib : LIBS) {
File libFile = new File(localPath.getAbsolutePath()
+ File.separator + lib);
if (!libFile.exists()) {
libMissing = true;
break;
}
}
if (!libMissing)
return; // We don't have to download
}
if (!localPath.exists() && !localPath.mkdirs()) // Error fatal!
throw new Exception("Can't create the path: " + localPath);
loadingScreen.setProgress(2, "Downloading library...");
File file = new File(localPath.getAbsolutePath() + File.separator
+ LIB_ARCHIVE);
String link = downloadPath + LIB_ARCHIVE;
download(link, file);
ZipFile zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
loadingScreen.setProgress(3, "Installing librarys..");
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory())
continue;
File tar = new File(localPath.getAbsolutePath() + File.separator
+ entry.getName());
InputStream is = zipFile.getInputStream(entry);
OutputStream os = new FileOutputStream(tar);
copyStream(is, os);
os.flush();
os.close();
is.close();
}
zipFile.close();
file.delete();
if (file.exists())
file.deleteOnExit();
}
I put the jar files on the server in a visible HTTP path
<applet code="com.colorfulwolf.webcamapplet.WebcamApplet"
archive="http://www.netimoveis.com/teste.jar, http://www.netimoveis.com/core.jar, http://www.netimoveis.com/javacv.jar, http://www.netimoveis.com/javase.jar, http://www.netimoveis.com/jna.jar, http://www.netimoveis.com/customizer.jar, http://www.netimoveis.com/jmf.jar, http://www.netimoveis.com/meidaplayer.jar, http://www.netimoveis.com/multiplayer.jar, http://www.netimoveis.com/sound.jar"
height="550" width="550">
</applet>
Why when I try to run the applet on the server, it does not leave the first step?
#UPDATE
I found the line where the code don't move to the next line of code.
String tmpDir = System.getProperty("java.io.tmpdir"); this line is where my code stop and still just in this line. Java is currently installed in server.